java.util提供了Date类来封装日期和时间。实例化Date类的两个构造函数,
Date();--当前时间
Date(long millisec);--距离格林威治时间1970年1月1日millisec毫秒的时间
1 public static void main(String[] args) {
// yyyy是年,MM是月,dd是日, HH是(24小时制)时,hh是(12小时制)时,mm是分,ss是秒 2 SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); 3 Date date = new Date(); 4 System.out.println(sdf.format(date)); 5 }
输出结果:2018/06/13 14:04:33
1 public static void main(String[] args) { 2 DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); 3 LocalDateTime date = LocalDateTime.now(); 4 System.out.println(dtf.format(date)); 5 }
输出结果:2018-06-13 14:31:29
1 public static void main(String[] args) { 2 String timeStr = "2018-06-13"; 3 DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd"); 4 LocalDate date = LocalDate.parse(timeStr, dtf); 5 System.out.println(date); 6 }
输出结果:2018-06-13
SimpleDateFormat与DateTimeFormatter的区别在于:DateTimeFormatter是线程安全的,而SimpleDateFormat并不是线程安全。
| 字母 | 描述 | 示例 | 
|---|---|---|
| G | 纪元标记 | AD | 
| y | 四位年份 | 2001 | 
| M | 月份 | July or 07 | 
| d | 一个月的日期 | 10 | 
| h | A.M./P.M. (1~12)格式小时 | 12 | 
| H | 一天中的小时 (0~23) | 22 | 
| m | 分钟数 | 30 | 
| s | 秒数 | 55 | 
| S | 毫秒数 | 234 | 
| E | 星期几 | Tuesday | 
| D | 一年中的日子 | 360 | 
| F | 一个月中第几周的周几 | 2 (second Wed. in July) | 
| w | 一年中第几周 | 40 | 
| W | 一个月中第几周 | 1 | 
| a | A.M./P.M. 标记 | PM | 
| k | 一天中的小时(1~24) | 24 | 
| K | A.M./P.M. (0~11)格式小时 | 10 | 
| z | 时区 | Eastern Standard Time | 
| ‘ | 文字定界符 | Delimiter | 
| " | 单引号 | ` | 
1     public static void main(String[] args) {
2         Calendar cal = Calendar.getInstance();
3         System.out.println(cal.get(Calendar.YEAR));//年
4         System.out.println(cal.get(Calendar.MONTH));//月(与当前月少一)
5         System.out.println(cal.get(Calendar.DATE));//日
6         System.out.println(cal.get(Calendar.HOUR_OF_DAY));//时
7         System.out.println(cal.get(Calendar.MINUTE));//分
8         System.out.println(cal.get(Calendar.SECOND));//秒
9     } 
Calendar类中用一下这些常量表示不同的意义,jdk内的很多类其实都是采用的这种思想
| 常量 | 描述 | 
|---|---|
| Calendar.YEAR | 年份 | 
| Calendar.MONTH | 月份 | 
| Calendar.DATE | 日期 | 
| Calendar.DAY_OF_MONTH | 日期,和上面的字段意义完全相同 | 
| Calendar.HOUR | 12小时制的小时 | 
| Calendar.HOUR_OF_DAY | 24小时制的小时 | 
| Calendar.MINUTE | 分钟 | 
| Calendar.SECOND | 秒 | 
| Calendar.DAY_OF_WEEK | 星期几 | 
如果只设定某个字段,例如日期的值,则可以使用如下set方法:
1 public static void main(String[] args) {
2         Calendar cal = Calendar.getInstance();
3         //设置年份为:2017年
4         cal.set(Calendar.YEAR,2017);
5         System.out.println(cal.getTime());
6         
7     } 
其他字段属性set的意义以此类推
1     public static void main(String[] args) {
2         Calendar cal = Calendar.getInstance();
3         //前一天的日期
4         cal.add(Calendar.DATE, -1);
5         System.out.println(cal.getTime());
6     } 
LocalDateTime 同样可以实现获取前一天的时间
1 public static void main(String[] args) {
2         LocalDateTime ldt = LocalDateTime.now();
3         LocalDateTime yesterday = ldt.minusDays(1);
4         System.out.println(yesterday);
5     }
Calendar类实现了公历日历,GregorianCalendar是Calendar类的一个具体实现。
Calendar 的getInstance()方法返回一个默认用当前的语言环境和时区初始化的GregorianCalendar对象。GregorianCalendar定义了两个字段:AD和BC。这些代表公历定义的两个时代。
下面列出GregorianCalendar对象的几个构造方法:
| 序号 | 构造函数和说明 | 
| 1 | GregorianCalendar() 在具有默认语言环境的默认时区内使用当前时间构造一个默认的 GregorianCalendar。 | 
| 2 | GregorianCalendar(int year, int month, int date) 在具有默认语言环境的默认时区内构造一个带有给定日期设置的 GregorianCalendar | 
| 3 | GregorianCalendar(int year, int month, int date, int hour, int minute) 为具有默认语言环境的默认时区构造一个具有给定日期和时间设置的 GregorianCalendar。 | 
| 4 | GregorianCalendar(int year, int month, int date, int hour, int minute, int second) 为具有默认语言环境的默认时区构造一个具有给定日期和时间设置的 GregorianCalendar。 | 
| 5 | GregorianCalendar(Locale aLocale) 在具有给定语言环境的默认时区内构造一个基于当前时间的 GregorianCalendar。 | 
| 6 | GregorianCalendar(TimeZone zone) 在具有默认语言环境的给定时区内构造一个基于当前时间的 GregorianCalendar。 | 
| 7 | GregorianCalendar(TimeZone zone, Locale aLocale) 在具有给定语言环境的给定时区内构造一个基于当前时间的 GregorianCalendar。 | 
这里是GregorianCalendar 类提供的一些有用的方法列表:
| 序号 | 方法和说明 | 
| 1 | void add(int field, int amount) 根据日历规则,将指定的(有符号的)时间量添加到给定的日历字段中。 | 
| 2 | protected void computeFields() 转换UTC毫秒值为时间域值 | 
| 3 | protected void computeTime() 覆盖Calendar ,转换时间域值为UTC毫秒值 | 
| 4 | boolean equals(Object obj) 比较此 GregorianCalendar 与指定的 Object。 | 
| 5 | int get(int field) 获取指定字段的时间值 | 
| 6 | int getActualMaximum(int field) 返回当前日期,给定字段的最大值 | 
| 7 | int getActualMinimum(int field) 返回当前日期,给定字段的最小值 | 
| 8 | int getGreatestMinimum(int field) 返回此 GregorianCalendar 实例给定日历字段的最高的最小值。 | 
| 9 | Date getGregorianChange() 获得格里高利历的更改日期。 | 
| 10 | int getLeastMaximum(int field) 返回此 GregorianCalendar 实例给定日历字段的最低的最大值 | 
| 11 | int getMaximum(int field) 返回此 GregorianCalendar 实例的给定日历字段的最大值。 | 
| 12 | Date getTime() 获取日历当前时间。 | 
| 13 | long getTimeInMillis() 获取用长整型表示的日历的当前时间 | 
| 14 | TimeZone getTimeZone() 获取时区。 | 
| 15 | int getMinimum(int field) 返回给定字段的最小值。 | 
| 16 | int hashCode() 重写hashCode. | 
| 17 | boolean isLeapYear(int year) 确定给定的年份是否为闰年。 | 
| 18 | void roll(int field, boolean up) 在给定的时间字段上添加或减去(上/下)单个时间单元,不更改更大的字段。 | 
| 19 | void set(int field, int value) 用给定的值设置时间字段。 | 
| 20 | void set(int year, int month, int date) 设置年、月、日的值。 | 
| 21 | void set(int year, int month, int date, int hour, int minute) 设置年、月、日、小时、分钟的值。 | 
| 22 | void set(int year, int month, int date, int hour, int minute, int second) 设置年、月、日、小时、分钟、秒的值。 | 
| 23 | void setGregorianChange(Date date) 设置 GregorianCalendar 的更改日期。 | 
| 24 | void setTime(Date date) 用给定的日期设置Calendar的当前时间。 | 
| 25 | void setTimeInMillis(long millis) 用给定的long型毫秒数设置Calendar的当前时间。 | 
| 26 | void setTimeZone(TimeZone value) 用给定时区值设置当前时区。 | 
| 27 | String toString() 返回代表日历的字符串。 | 
public static void main(String[] args) { Calendar.getInstance().getTimeInMillis(); System.currentTimeMillis(); Clock.systemDefaultZone().millis(); SimpleDateFormat sdf = new SimpleDateFormat(); System.out.println(sdf.format(Calendar.getInstance().getTimeInMillis())); System.out.println(sdf.format(System.currentTimeMillis())); System.out.println(sdf.format(Clock.systemDefaultZone().millis()));
}
参考文章:https://www.cnblogs.com/blackheartinsunshine/p/6019408.html
原文:https://www.cnblogs.com/zy-l/p/9178234.html