ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Java日期时间处理总结

2021-10-18 13:00:36  阅读:199  来源: 互联网

标签:总结 Java 日期 时间 2021 LocalDateTime Date java Calendar


java.util.Date、java.util.Calendar(jdk1.8前)、java.time.LocalDate(jdk1.8后线程安全)

Date类

// 1626624005531  时间戳为Long类型,适用于计算时间差
Long time = System.currentTimeMillis();

//时间戳和Date类互相转换
Date date = new Date();
Long time = date.getTime();
// 时间戳转换为 Date 类型
Long long = 1626624005531L;
Date nowTime = new Date(long);

//java.util.Date 类与 java.text.SimpleDateFormat 类一起用,用于格式化日期
//getDateTimeInstance(),getDateInstance(),getTimeInstance()静态方法获取标准日期格式
System.out.println(new Date());                   // Thu Jul 29 10:30:04 CST 2021  
String s = SimpleDateFormat.getDateTimeInstance().format(new Date());   //2021-7-29 10:30:04
//将字符串转换为日期
String timeOne = "2021-07-18 22:53:22";
Date date = SimpleDateFormat.getDateTimeInstance().parse(timeOne);

Calendar类

java.util.Calendar:日历类,是个抽象类
Calendar 实例化的方法:

  1. 创建其子类 GregorianCalendar的对象
  2. 调用其静态方法 getInstance()
//两种方法本质上相同,调用静态方法获取的对象就是子类的对象
Calendar calendar = Calendar.getInstance();
// class java.util.GregorianCalendar
System.out.println(calendar.getClass());

常用方法:
● get(int field):获取某个时间
● set():设置某个时间
● add():在某个时间上,加上某个时间
● getTime():将 Calendar 类转换为 Date 类
● setTime():将 Date 类转换为 Calendar 类
field 取值有:
● Calendar.DAY_OF_MONTH:这个月的第几天
● Calendar.DAY_OF_YEAR:这年的第几天

//get()获取当前时间
int i = calendar.get(Calendar.DAY_OF_MONTH);
//set(int field, int value)设置当前时间
calendar.set(Calendar.DAY_OF_MONTH, 23);
//add(int field, int value)给当前时间添加时间
calendar.add(Calendar.DAY_OF_MONTH, 5);

//和Date类相互转化
Date time = calendar.getTime();
calendar.setTime(new Date());

java.time.LocalDate(jdk1.8后线程安全)

https://blog.csdn.net/sco5282/article/details/118914681
java.time 中包含了:
● LocalDate类:本地日期。yyyy-MM-dd 格式的日期。可以存储生日、纪念日等
● LocalTime类:本地时间。代表的是时间,不是日期
● LocalDateTime类:本地日期时间
● ZonedDateTime类:时区
● Duration类:持续时间
java.time常用API:
● now():获取当前时间、日期
● of():获取指定的日期、时间
● getXxx():获取值
● withXxx():设置值
● plusXxx():加
● minusXxx():减
格式化:先获取格式对象
format对象.format() 时间对象转换为字符串
format对象.parse() 字符串转化为时间对象

System.out.println(LocalDateTime.now());   //2021-07-29T10:53:35.429
LocalDateTime dateTime = LocalDateTime.of(2021, 7, 19, 21, 50, 0);  //2021-07-19T21:50

System.out.println(localDateTime.getDayOfMonth());     // 19
System.out.println(localDateTime.getMonth());          // JULY

//具有不可变性,设置后返回对象为新对象
LocalDateTime dayOfMonth = localDateTime.withDayOfMonth(25);    //2021-07-25T10:58:10.903
System.out.println(localDateTime);                              //2021-07-29T10:58:10.903
LocalDateTime l = now.withYear(2002).withDayOfMonth(2).withMonth(2);

LocalDateTime months = localDateTime.plusMonths(5);

//将时间转换为字符串格式   FormatStyle.MEDIUM表示格式 2021-7-29 11:08:03 
String format = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM).format(LocalDateTime.now());
//自定义格式    2021-07-29 11:08:03  
String format1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now());
//字符串转化为时间
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime parse = LocalDateTime.parse("2021-07-29 11:27:08", dateTimeFormatter);

mysql中比较时间大小

前端传来的都是字符串格式,如果类似于2020-01-13T16:00:00.000Z,是世界标准时间,使用DateFormatUtils转换
● 时间类型:直接用 between and
● 字符串 ->时间: STR_TO_DATE(report_time,'%Y-%m-%d %H:%i:%s')
● 时间戳 ->时间: from_unixtime(1592755199)

标签:总结,Java,日期,时间,2021,LocalDateTime,Date,java,Calendar
来源: https://www.cnblogs.com/centralpark/p/15420151.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有