博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java时间类记录
阅读量:4298 次
发布时间:2019-05-27

本文共 2596 字,大约阅读时间需要 8 分钟。

文章目录

Java时间类记录

一、前言

  • 环境说明:

JDK:1.8

官方API文档:

二、记录

  • java.util.Date

参考:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");Date date = new Date();System.out.println(sdf.format(date));
  • java.util.Calendar

参考:

Calendar calendar = Calendar.getInstance();int year = calendar.get(Calendar.YEAR);int month = calendar.get(Calendar.MONTH) + 1;int day = calendar.get(Calendar.DATE);// int hour = calendar.get(Calendar.HOUR); // 12小时制int hour = calendar.get(Calendar.HOUR_OF_DAY); // 24小时制int minute = calendar.get(Calendar.MINUTE);int second = calendar.get(Calendar.SECOND);System.out.println(year +"-"+ month +"-"+ day +" "+ hour +":"+ minute +":"+ second);// orSimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");System.out.println(sdf.format(calendar.getTime()));
  • java.time.LocalDatejava.time.LocalTimejava.time.LocalDateTime

JDK:1.8+

参考:

DateTimeFormatter dateDTF = DateTimeFormatter.ofPattern("yyyy-MM-dd");LocalDate localDate = LocalDate.now();DateTimeFormatter timeDTF = DateTimeFormatter.ofPattern("HH:mm:ss");LocalTime localTime = LocalTime.now();System.out.println(localDate.format(dateDTF) +" "+ localTime.format(timeDTF));// orDateTimeFormatter datetimeDTF = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");LocalDateTime localDateTime = LocalDateTime.now();System.out.println(localDateTime.format(datetimeDTF));
  • System.currentTimeMillis()

参考:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");Date date = new Date(System.currentTimeMillis());System.out.println(sdf.format(date));
  • java.sql.Timestamp

参考:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");Date date = new Date();Timestamp timestamp = new Timestamp(date.getTime());System.out.println(sdf.format(timestamp));// ortimestamp = new Timestamp(System.currentTimeMillis());date = new Date(timestamp.getTime());System.out.println(sdf.format(date));
  • java.sql.Date

参考:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");Date date = new Date();java.sql.Date sqlDate = new java.sql.Date(date.getTime());System.out.println(sdf.format(sqlDate));// orsqlDate = new java.sql.Date(System.currentTimeMillis());date = new Date(sqlDate.getTime());System.out.println(sdf.format(date));

三、其它

1.获取未来或过去24小时的时间

SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");Calendar calendar = Calendar.getInstance();calendar.setTime(new Date()); // 设置当前时间for(int i=0; i< 24; i++){
String time = sdf.format(calendar.getTime()); System.out.println(time); // 如果 -1 则为是过去24小时的时间 calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) + 1);}

转载地址:http://swnws.baihongyu.com/

你可能感兴趣的文章
OpenGL ES 3.0(八)实现带水印的相机预览功能
查看>>
OpenGL ES 3.0(九)实现美颜相机功能
查看>>
FFmpeg 的介绍与使用
查看>>
Android 虚拟机简单介绍——ART、Dalvik、启动流程分析
查看>>
原理性地理解 Java 泛型中的 extends、super 及 Kotlin 的协变、逆变
查看>>
FFmpeg 是如何实现多态的?
查看>>
FFmpeg 源码分析 - avcodec_send_packet 和 avcodec_receive_frame
查看>>
FFmpeg 新旧版本编码 API 的区别
查看>>
RecyclerView 源码深入解析——绘制流程、缓存机制、动画等
查看>>
Android 面试题整理总结(一)Java 基础
查看>>
Android 面试题整理总结(二)Java 集合
查看>>
学习笔记_vnpy实战培训day02
查看>>
学习笔记_vnpy实战培训day03
查看>>
VNPY- VnTrader基本使用
查看>>
VNPY - CTA策略模块策略开发
查看>>
VNPY - 事件引擎
查看>>
MongoDB基本语法和操作入门
查看>>
学习笔记_vnpy实战培训day04_作业
查看>>
OCO订单(委托)
查看>>
学习笔记_vnpy实战培训day05
查看>>