我们先来了解一下基本的概念

日期

  • 2020-11-21
  • 2020-11-22

时间

  • 15:36:43
  • 2020-11-21 15:36:43

时区

  • 北京时间 2020-11-21 15:36:43
  • 东京时间 2020-11-21 16:36:43

地区

Locale表示一个国家或地区的日期、时间、数字、货币等格式

时间

  • zh_CN : 2020-11-21 //表示中国的locale
  • en_US : 21/11/2020 //表示纽约的locale

价格

  • 12000.00 //中国
  • 12,000.00 //美国

现在介绍第一个类:Calendar

Calendar这个类用来设置或者获取年、月、日、时、分、秒

public class CalendarTest {
public static void main(String[] args) { //获取当前时间的一个Calendar对象
Calendar calendar = Calendar.getInstance(); //获取年
int y = calendar.get(Calendar.YEAR); //获取月
int m = calendar.get(Calendar.MONTH); //获取日
int d = calendar.get(Calendar.DAY_OF_MONTH); //获取当前星期
int w = calendar.get(Calendar.DAY_OF_WEEK); //获取小时
int hh = calendar.get(Calendar.HOUR_OF_DAY); //获取分钟
int mm = calendar.get(Calendar.MINUTE); //获取秒
int ss = calendar.get(Calendar.SECOND); //获取毫秒
int ms = calendar.get(Calendar.MILLISECOND); }
}
  • Date getTime()
  • long getTimelnMillis()

我们还可以设置指定时间

public class CalendarTest {
public static void main(String[] args) { //获取当前时间的一个Calendar对象
Calendar calendar = Calendar.getInstance();
//可以通过 setTime(Date)设置时间
calendar.setTime(new Date());
//也可以通过 setTimeInMillis(long) System.currentTimeMillis() 获取当前系统时间 返回值为long类型
calendar.setTimeInMillis(System.currentTimeMillis());
//我们还可以清楚掉时间自己设置
calendar.clear();
//设置年
calendar.set(Calendar.YEAR,1999);
//设置月
calendar.set(Calendar.MONTH,10);
//设置日期
calendar.set(Calendar.DAY_OF_MONTH,24);
//设置小时
calendar.set(Calendar.HOUR_OF_DAY,11); System.out.println(calendar.getTime()); }
}

时区转换

calendar.setTimeZone(TimeZone.getTimeZone("America/New_York"));

加减时间

        //获取当前时间的一个Calendar对象
Calendar calendar = Calendar.getInstance(); //加五天
calendar.add(Calendar.DAY_OF_MONTH,5); //减2天
calendar.add(Calendar.DAY_OF_MONTH,-2);

我们再来讲第二个类:LocalDateTime(新)

LocalDate(本地的日期)

LocalTime(本地的时间)

LocalTime(本地的日期和时间)

特点有

  • 严格区分日期、时间
  • 不变类(类似String)
  • Month范围1-12(Jan-Dec)
  • Week范围1-7(Mon-Sun)
        //当前日期
LocalDate localDate = LocalDate.now(); //当前时间
LocalTime localTime = LocalTime.now(); //当前日期和时间
LocalDateTime localDateTime = LocalDateTime.now(); //指定日期和时间
LocalDate date = LocalDate.of(2020, 11, 21);
LocalTime time = LocalTime.of(21, 54, 12); LocalDateTime dateTime1 = LocalDateTime.of(2020, 11, 21, 21, 54, 12);
LocalDateTime dateTime2 = LocalDateTime.of(date, time);

格式化日期Formatter

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        System.out.println(dateTimeFormatter.format(LocalDateTime.now()));
//打印结果
2020-11-21 22:00:19

默认按照ISO标准化进行格式化和解析

  • yyyy-MM-dd
  • HH:mm:ss
  • HH:mm:ss.SSSS
  • yyyy-MM-dd HH:mm:ss
  • yyyy-MM-dd HH:mm:ss.SSSS

日期和时间的运算

        LocalDate today = LocalDate.now();

        //+5天
LocalDate localDate = today.plusDays(5); //-2小时
LocalDateTime localDateTime = LocalDateTime.now();
LocalDateTime localDateTime1 = localDateTime.minusHours(2); //+1月 -2周
LocalDate date = today.plusMonths(1).minusWeeks(2); //本月的第一天
LocalDate.now().withDayOfMonth(1); //本月的最后一天
LocalDate.now().with(TemporalAdjusters.lastDayOfMonth()); //本月的第一个周日
LocalDate.now().with(TemporalAdjusters.firstInMonth(DayOfWeek.SUNDAY));

更多的运算方法你们可以ctrl + 鼠标左键查看LocalDateTime的源码

判断日期和时间的先后

  • isBefore()
  • isAfter()
  • equals()
        LocalDate localDate1 = LocalDate.of(2020, 11, 21);
LocalDate localDate2 = LocalDate.of(2020, 11, 20); //检查此日期是否早于指定日期
System.out.println(localDate1.isBefore(localDate2));
//检查此日期是否在指定的日期之后
System.out.println(localDate1.isAfter(localDate2));
//判断日期是否相等
System.out.println(localDate1.equals(localDate2));

计算两个日期之差

LocalDate localDate1 = LocalDate.of(2020, 11, 21);
LocalDate localDate2 = LocalDate.of(2079, 4, 20); //获取两个日期之间的差作为Period 对象返回
Period period = localDate1.until(localDate2); //打印结果为P58Y4M30D 表示相差 58年4个月30天
System.out.println(period);
//获取年 58
System.out.println(period.getYears());
//获取月 4
System.out.println(period.getMonths());
//获取天 30
System.out.println(period.getDays());

LocalDateTime无法与弄进行转换

  • 因为LocalDateTime没有时区,无法确定某一时刻
  • ZonedDateTime有时区,可以与long进行转换

那我们就来讲讲ZonedDateTime:带时区的日期和时间

  • ZonedDateTime:带时区的日期和时间
  • ZoneId:新的时区对象(取代TimeZone)
  • Instant:时刻对象
        //当前时区的日期和时间
ZonedDateTime localDateTime = ZonedDateTime.now(); //纽约时区的日期和时间
ZonedDateTime zonedDateTime = ZonedDateTime.now(ZoneId.of("America/New_York"));

可以从LocalDateTime转换

  • atZone()
LocalDateTime localDateTime = LocalDateTime.of(2020, 11, 21, 8, 3, 3);

        //关联到当前默认时区
ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.systemDefault()); //关联到纽约时区
ZonedDateTime zonedDateTime1 = localDateTime.atZone(ZoneId.of("America/New_York"));

Instant对象表示时刻

        Instant instant = ZonedDateTime.now().toInstant();
long epochSecond = instant.getEpochSecond();//返回long 可以与long进行转换

这里再额外提一点 新旧API中时间类型对应的数据库时间类型

数据库 对应Java类(旧) 对应Java类(新)
DATETIME java.util.Date LocalDateTime
DATE java.sql,Date LocalDate
TIME java.sql.Time LocalTime
TIMESTAMP java.sql.Timestamp LocalDateTime

再附送一个开箱即用的根据不同时区显示不同时间的的工具方法

    /**
* 根据不同时区显示不同的时间
* @param epoch
* @param locale
* @param zoneId
* @return
*/
public String epochToString(long epoch, Locale locale, String zoneId) {
Instant instant = Instant.ofEpochMilli(epoch);
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofLocalizedDateTime(
FormatStyle.MEDIUM, FormatStyle.SHORT
);
return dateTimeFormatter.withLocale(locale).format(ZonedDateTime.ofInstant(instant, ZoneId.of(zoneId)))
}

最新文章

  1. .net session_end
  2. iOS 关于修饰代理用weak还是assign
  3. 将HTML5封装成android应用APK文件的几种方法(转载)
  4. python 练习(一)代码统计工具的实现
  5. memcached 系列2:memcached实例(转载)
  6. JS中document.createElement()用法及注意事项
  7. Ajax页面跳转
  8. java面试题及答案(基础题122道,代码题19道)
  9. winform —— 界面
  10. JSON基础学习
  11. python selenium模拟滑动操作
  12. Redis数据类型SortedSET
  13. kaptcha验证码的使用
  14. maven多个web模块进行合并
  15. Physics Experiment 弹性碰撞 [POJ3684]
  16. 解决phpstudy在 cmd窗口输出 php5 中文显示乱码问题
  17. fetch get方法的时候报错
  18. tp剩余未验证内容-8
  19. SharePoint 2013 APP 开发示例 (一)List 读写
  20. ROS中的CMakeLists.txt

热门文章

  1. windows下python3和python2虚拟环境配置
  2. Backdrop Filter
  3. Redis 设计与实现 5:压缩列表
  4. CentOS7下常用安装服务软件源码编译安装方式的介绍
  5. Spring Boot使用Maven自定义打包方式
  6. SQL语句中case,when,then的用法
  7. idea中快捷键换成熟悉的celipse中快捷键
  8. session在什么时候创建,以及session一致性问题
  9. Flash Player的终章——赠予它的挽歌
  10. device_create为何可以在/sys/class/具体类/属性文件?怎么实现的