LocalDateTime获取 年月日时分秒和判断日期大小
环境:java version "13.0.1"。
创建一个DateUtils类,提供三个常用方法:
String 转换 LocalDateTime的方法。
获取LocalDateTime获取 年月日时分秒。
两个时间的间隔毫秒数,常用于判断日期大小。
import org.springframework.util.StringUtils;
import java.text.DateFormat;
import java.time.DayOfWeek;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
public class DateUtils {
private static DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss:SSS");
public static void getTimeByDate() {
Date date = new Date();
DateFormat df1 = DateFormat.getDateInstance();//日期格式,精确到日
System.out.println(df1.format(date));
DateFormat df2 = DateFormat.getDateTimeInstance();//可以精确到时分秒
System.out.println(df2.format(date));
DateFormat df3 = DateFormat.getTimeInstance();//只显示出时分秒
System.out.println(df3.format(date));
DateFormat df4 = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL); //显示日期,周,上下午,时间(精确到秒)
System.out.println(df4.format(date));
DateFormat df5 = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG); //显示日期,上下午,时间(精确到秒)
System.out.println(df5.format(date));
DateFormat df6 = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT); //显示日期,上下午,时间(精确到分)
System.out.println(df6.format(date));
DateFormat df7 = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM); //显示日期,时间(精确到分)
System.out.println(df7.format(date));
}
public static void getTimeByCalendar() {
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);//获取年份
int month = cal.get(Calendar.MONTH);//获取月份
int day = cal.get(Calendar.DATE);//获取日
int hour = cal.get(Calendar.HOUR);//小时
int minute = cal.get(Calendar.MINUTE);//分
int second = cal.get(Calendar.SECOND);//秒
int WeekOfYear = cal.get(Calendar.DAY_OF_WEEK);//一周的第几天
System.out.println("现在的时间是:公元" + year + "年" + month + "月" + day + "日," + hour + "时" + minute + "分" + second + "秒, 一周的第几天:" + WeekOfYear);
}
/**
* 获取指定时间的年月日等
* @param localDateTime 指定时间
*/
public static void getTimeByGivenLocalDateTime(LocalDateTime localDateTime) {
if (null == localDateTime) {
localDateTime = LocalDateTime.now();
}
int dayOfYear = localDateTime.getDayOfYear();
int dayOfMonth = localDateTime.getDayOfMonth();
DayOfWeek dayOfWeek = localDateTime.getDayOfWeek();
System.out.println("今天是" + localDateTime + "\n"
+ "本年当中第" + dayOfYear + "天" + "\n"
+ "本月当中第" + dayOfMonth + "天" + "\n"
+ "本周中星期" + dayOfWeek.getValue() + "-即" + dayOfWeek + "\n");
//获取指定时间的年月日时分秒
int year = localDateTime.getYear();
Month month = localDateTime.getMonth();
int day = localDateTime.getDayOfMonth();
int hour = localDateTime.getHour();
int minute = localDateTime.getMinute();
int second = localDateTime.getSecond();
System.out.println("今天是" + localDateTime + "\n"
+ "年 : " + year + "\n"
+ "月 : " + month.getValue() + "-即 " + month + "\n"
+ "日 : " + day + "\n"
+ "时 : " + hour + "\n"
+ "分 : " + minute + "\n"
+ "秒 : " + second + "\n"
);
}
/**
* String 转换 LocalDateTime
* @param dateStr
* @return
*/
public static LocalDateTime string2Time(String dateStr) {
if (StringUtils.isEmpty(dateStr)) {
return null;
}
LocalDateTime time = LocalDateTime.parse(dateStr, fmt);
System.out.println("time:" + time);
return time;
}
/**
* 求时间间隔,返回值大于零说明结束时间晚于开始时间
* @param beginTime 开始时间
* @param endTime 结束时间
* @return 毫秒,等于endTime 减去 beginTime后的毫秒数
*/
public static Long between(LocalDateTime beginTime, LocalDateTime endTime) {
Duration duration = Duration.between(beginTime, endTime);
return duration.toMillis();
}
public static void main(String[] args) {
getTimeByDate();
System.out.println("********getTimeByCalendar********************");
getTimeByCalendar();
System.out.println("********getTimeByGivenLocalDateTime****************");
String str = "2021-10-09 15:32:06:777";
LocalDateTime ldt = string2Time(str);
getTimeByGivenLocalDateTime(ldt);
System.out.println("********between****************");
str = "2021-10-09 17:32:06:777";
LocalDateTime endTime = string2Time(str);
System.out.println(between(ldt, endTime));
}
}
测试结果:
2021年10月10日
2021年10月10日 下午12:06:02
下午12:06:02
2021年10月10日星期日 中国标准时间 下午12:06:02
2021年10月10日 CST 下午12:06:02
2021/10/10 下午12:06
2021年10月10日 下午12:06:02
********getTimeByCalendar********************
现在的时间是:公元2021年9月10日,0时6分2秒, 一周的第几天:1
********getTimeByGivenLocalDateTime****************
time:2021-10-09T15:32:06.777
今天是2021-10-09T15:32:06.777
本年当中第282天
本月当中第9天
本周中星期6-即SATURDAY
今天是2021-10-09T15:32:06.777
年 : 2021
月 : 10-即 OCTOBER
日 : 9
时 : 15
分 : 32
秒 : 6
********between****************
time:2021-10-09T17:32:06.777
7200000
Process finished with exit code 0
LocalDateTime获取 年月日时分秒和判断日期大小的更多相关文章
- Sql 中获取年月日时分秒的函数
getdate():获取系统当前时间 dateadd(datepart,number,date):计算在一个时间的基础上增加一个时间后的新时间值,比如:dateadd(yy,30,getdate()) ...
- jquery获取年月日时分秒当前时间
获取年月日时分秒的当前时间,可按照某种格式显示出来,下面是一种得到如2017年02月02日 00:00:00格式的当前时间 function getCurrentDate(date){ var y ...
- java Date获取 年月日时分秒
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...
- JS获取年月日时分秒
var d = new Date(); ) + "-" + d.getDate() + " " + d.getHours() + ":" + ...
- 2018.2.2 java中的Date如何获取 年月日时分秒
package com.util; import java.text.DateFormat; import java.util.Calendar; import java.util.Date; pub ...
- Android Calendar获取年月日时分秒毫秒
开始使用new Date()测试,并用通过date.getMonth(),和date.getDay()获取,不过后来发现这两个访求是jdk1.1版本的,现在已经不用了,而且结果也不正确. ; int ...
- TEST===>Sqlserver中获取年月日时分秒
可以用两种方法获取 1. select GETDATE() as '当前日期', DateName(year,GetDate()) as '年', DateName(month,GetDate()) ...
- android获取年月日时分秒
Calendar calendar=Calendar.getInstance(); //获取当前时间,作为图标的名字 String year=calendar.get(Calendar.YEAR)+& ...
- python 获取年月日时分秒 获取当前时间 datetime函数
import datetime#取当前时间print(datetime.datetime.now())#取年print(datetime.datetime.now().year)#取月print(da ...
- js 获取年月日时分秒,星期
getDate() { var date = new Date() // 获取时间 var year = date.getFullYear() // 获取年 var month = date.getM ...
随机推荐
- faker 函数支持哪些
3.2 常用函数 除了上述介绍的fake.name和fake.address生成姓名和地址两个函数外,常用的faker函数按类别划分有如下一些常用方法. 1.地理信息类 fake.city_suffi ...
- angular+ionic项目,页面无法滚动的问题
在做angular+ionic+cordova项目时,遇到一个小小的问题,就是内容做完,页面无法滚动,导致内容显示不完整 首先我检查了样式,发现并没有给页面定死高度,再次检查结构发现,我并没有用ion ...
- phpstorm、goland常用快捷键
1) 文件操作相关的快捷键 快捷键 作用 Ctrl + E 打开最近浏览过的文件 Ctrl + N 快速打开某个 struct 结构体所在的文件 Ctrl + Shift + N 快速打开文件 Shi ...
- Django实战项目-学习任务系统-配置定时调度任务
接着上期代码内容,继续完善优化系统功能. 本次增加配置定时调度任务功能,学习任务系统定时任务管理添加的定时学习任务,需要通过配置调度任务,定时发布周期性的学习任务. 以及每天定时发送学生用户属性值,积 ...
- IDA Pro 初步实践
实践1 背景 某软件A,在非全屏显示时带有常规菜单,在全屏下没有常规菜单,但是有顶部工具条,工具条上有菜单和按钮.对于全屏和非全屏的切换可以通过菜单,也可以通过快捷键ctrl + alt + ente ...
- 通过 openpyxl 操作 excel 表格
博客地址:https://www.cnblogs.com/zylyehuo/ STEP1: 导入相关库 import os from openpyxl import load_workbook STE ...
- 如何在linux中查看cpu信息、机器硬件型号
# cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c 8 Intel(R) Xeon(R) CPU E5410 @ 2.33GHz (看到有8 ...
- linux 上安装portainer.io
inux 上安装portainer.io 1.portainer.io是什么? 2.安装,运行镜像 3.登陆1.portainer.io是什么?Portainer是一个轻量级的Doc ...
- Delphi CheckListBox 用法
for i := CheckListBox1.Items.Count-1 downto 0 do //从后面往前面删 begin if CheckListBox1.Checked[i] then // ...
- 异常--java进阶day08
1.异常 java中,所有的异常都是类 2.异常的体系结构 3.编译时异常与运行时异常 1.编译时异常 语法完全正确,但是代码就是会报错,如下图 上图中,写的是时间格式化类的使用,parse方法将给的 ...