java中获得时间及个别操作



// 博客搬家咯~ http://joeleee.github.io/


// 博客搬家咯~ http://joeleee.github.io/


// 博客搬家咯~ http://joeleee.github.io/




public
static void main(String[] args) {

SimpleDateFormat matter = new SimpleDateFormat("yyyy-MM-dd_HH:mm"); // 自定义时间格式

Calendar calendar = Calendar.getInstance(); // 获得当前时间
Date date1 = calendar.getTime(); // 将calendar的时间赋值给date1
calendar.add(Calendar.MINUTE, -100); // 将calendar的时间减去100分钟
Date date2 = calendar.getTime(); // 将calendar的时间赋值给date2
Date date3 = null;

try {
date3 = matter.parse("1989-05-18_10:21"); // 按照自定义的格式自定时间
} catch (ParseException e) {
e.printStackTrace();
}
String str1 = matter.format(date1); // 将date1格式化为自定义时间格式的字符串
String str2 = matter.format(date2); // 将date2格式化为自定义时间格式的字符串
String str3 = matter.format(date3); // 将date3格式化为自定义时间格式的字符串

System.out.println(str1); // 输出
System.out.println(str2); // 输出
System.out.println(str3); // 输出
System.out.println(date1.after(date2)); // 比较date1和date2

System.exit(0);
return;
}
From zhuocheng (cnblogs.com/zhuocheng)
原文地址:https://www.cnblogs.com/zhuocheng/p/2351084.html