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 ...
随机推荐
- 【Jmatpro 10.0】根据材料牌号输出应力-应变曲线
1.前提条件 Jmatpro 软件 一个材料牌号或者材料成分数据 2. 步骤 以 X15Cr 13牌号金属为案例 材料数据来自:材数库 运行jmatpro软件 选择 material type:sta ...
- python py文件名称不能和库名称一样,否则报错module 'requests' has no attribute 'post'
这个问题自己犯过几次,加深一下记忆
- windows本地认证
windows本地认证 本地认证概述 本地认证最简单的例子就是我们的电脑上存储着自己的账号密码,无论电脑是否联网,只要能开机,就可以输入账号密码登录到电脑中,工作组就是采用本地认证. 那认证流程是什么 ...
- python 读写csv文件(创建,追加,覆盖)
读取csv文件 利用 csv.reader 可以读 csv 文件,然后返回一个可迭代的对象 csv_read,我们可以直接从 csv_read 中取数据 import csv def read_csv ...
- JS处理数据四舍五入(tofixed与round的区别详解)
1 .tofixed方法 toFixed() 方法可把 Number 四舍五入为指定小数位数的数字.例如将数据Num保留2位小数,则表示为:toFixed(Num):但是其四舍五入的规则与数学中的规则 ...
- IDA Pro分析dll在exe中的表现
尝试分析隐式加载和显式加载的dll在exe中进行反汇编时的表现. 1. 测试条件 (1)动态库 testdll1 导出函数 int add(int, int) 和 int add2(int, in ...
- 【前端JSP思考】JSP中#{},${}和%{}的区别
JSP中#{},${}和%{}的区别: #{} #{}:对语句进行预编译,此语句解析的是占位符?,可以防止SQL注入, 比如打印出来的语句 select * from table where id=? ...
- 【HTML】步骤进度条组件
HTML步骤进度条 效果图 思路 分份: 有多少个步骤就可以分成多少分,每份宽度应该为100%除以步骤数,故以上效果图中的每份宽度应该为25%,每份用一个div. 每份: 每份中可以看成是三个元素,一 ...
- CoreOS 手动更新
以阿里云 ECS 安装的 CoreOS 为例,你家装的 CoreOS 基本也一样啦. 查看和修改更新组 第一个问题:"什么是更新组?",请先看CoreOS 发行版本介绍 # 查看更 ...
- ubuntu 安装挂载mysql, redis和mongodb服务
因为有两台电脑,所以准备把ubuntu电脑作成对外服务提供,各种数据库,中间件都使用docker安装管理,然后挂载配置和日志到本地,提供给另一台电脑的对外服务. 1. 安装docker sudo ap ...