java常用时间类

java常用时间类

//1、java.lang.System
System.out.println(System.currentTimeMillis());//获取当前本地时间戳:1630374564896

//2、java.util.Date
Date date = new Date();
Date date2 = new Date(1630305388904L);
System.out.println(date.getTime());//获取当前本地时间戳:1630374564898
System.out.println(date.toString());//获取当前本地时间戳转换为:星期 月 日 时:分:秒 时间标准 年  Tue Aug 31 09:49:24 CST 2021

//3、java.sql.Date
java.sql.Date date3 = new java.sql.Date(1630305388904L);
System.out.println(date3);//年-月-日 2021-08-30

//4、java.text.SimpleDateFormat
//对Date格式化为字符串日期,解析字符串日期
SimpleDateFormat sdf1 = new SimpleDateFormat();
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy.MMMM.dd GGG hh:mm aaa");
SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd  hh:mm:ss");
//格式化
System.out.println(sdf1.format(date));//21-8-31 上午9:43
System.out.println(sdf2.format(date));//2021.八月.31 公元 09:43 上午
System.out.println(sdf3.format(date));//2021-08-31  09:44:57
//解析
System.out.println(sdf1.parse("21-8-31 上午9:38"));              //Tue Aug 31 09:38:00 CST 2021
System.out.println(sdf2.parse("2021.八月.31 公元 09:43 上午"));   //Tue Aug 31 09:43:00 CST 2021
System.out.println(sdf3.parse("2021-08-31  09:44:57"));         //Tue Aug 31 09:44:57 CST 2021

//5、java.util.Calendar
Calendar calendar = Calendar.getInstance();
System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
calendar.set(Calendar.DAY_OF_MONTH,23);
System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
calendar.add(Calendar.DAY_OF_MONTH,4);
System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
System.out.println(calendar.getTime());//日历类-->时间
calendar.setTime(new Date());           //时间-->日历类
System.out.println(calendar.get(Calendar.DAY_OF_MONTH));

//6、LocalDate、LocalTime、LocalDateTime
//now
System.out.println(LocalDate.now());//2021-08-31
System.out.println(LocalTime.now());//11:15:19.621
System.out.println(LocalDateTime.now());//2021-08-31T11:15:19.621
//of
LocalDateTime localDateTime = LocalDateTime.of(2021, 8, 31, 13, 23, 23, 123);
System.out.println(localDateTime);//2021-08-31T13:23:23.000000123
//getXxx
System.out.println(localDateTime.getDayOfMonth());//31
System.out.println(localDateTime.getDayOfWeek());//TUESDAY
System.out.println(localDateTime.getMonth());//AUGUST
System.out.println(localDateTime.getMonthValue());//8
System.out.println(localDateTime.getMinute());//23
//withXxx:不可变性
LocalDateTime localDateTime1 = localDateTime.withDayOfMonth(22);
System.out.println(localDateTime);//2021-08-31T13:23:23.000000123
System.out.println(localDateTime1);//2021-08-22T13:23:23.000000123
//plusXxx/minusXxx
System.out.println(localDateTime.plusHours(3));//2021-08-31T16:23:23.000000123
System.out.println(localDateTime.minusMonths(1));//2021-07-31T13:23:23.000000123

//7、Instant
Instant instant = Instant.now();
System.out.println(instant);//2021-08-31T06:20:33.213Z
OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
System.out.println(offsetDateTime);//2021-08-31T14:20:33.213+08:00
System.out.println(instant.toEpochMilli());//1630390992420
System.out.println(Instant.ofEpochMilli(1630390992420l));//2021-08-31T06:23:12.420Z

//8、java.time.format.DateTimeFormatter
//ISO_LOCAL_DATE_TIME\ISO_LOCAL_DATE\ISO_LOCAL_TIME
DateTimeFormatter isoLocalDateTime = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
LocalDateTime localDateTime2 = LocalDateTime.now();
System.out.println(localDateTime2);//2021-08-31T14:32:53.956
System.out.println(isoLocalDateTime.format(localDateTime2));//2021-08-31T14:32:53.956
System.out.println(isoLocalDateTime.parse("2021-08-31T14:32:53.956"));//{},ISO resolved to 2021-08-31T14:32:53.956
//ofLocalizedDateTime(FormatStyle.LONG|MEDIUM|SHORT|FULL)
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);
System.out.println(dateTimeFormatter.format(localDateTime2));//2021年8月31日 下午02时38分44秒
System.out.println(dateTimeFormatter.parse("2021年8月31日 下午02时38分44秒"));//{},ISO resolved to 2021-08-31T14:38:44
//ofPattern(“yyyy-MM-dd hh:mm:ss”)
DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
System.out.println(dateTimeFormatter1.format(LocalDateTime.now()));//2021-08-31 02:46:55
System.out.println(dateTimeFormatter1.parse("2021-08-31  
//{MinuteOfHour=46, MicroOfSecond=0, MilliOfSecond=0, SecondOfMinute=55, HourOfAmPm=2, NanoOfSecond=0}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值