Java日期操作与判断



计算两个date间相差天数

1
2
// 24小时 * 60分 * 60秒 * 1000毫秒 = 86400000毫秒
long days = (date.getTime() - nowDate.getTime()) / (24 * 60 * 60 * 1000);

工作日计算工具类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public static Date getDate(Date currentDate, int days) {
if (days == 0) {
return currentDate;
}

Calendar calendar = Calendar.getInstance();
calendar.setTime(currentDate);
// 判断是加天数还是减去
int step = days < 0 ? -1 : 1;
int i = 0;
int daysAbs = Math.abs(days);
while (i < daysAbs) {
calendar.add(Calendar.DATE, step);
i++;
// Calendar.SATURDAY:7 星期六,Calendar.SUNDAY:1 星期天
if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY
|| calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
// 如果遇到一次双休 加1
i--;
}
}

return calendar.getTime();
}

生成指定的 Date 类型

1
2
3
4
ZoneId zoneId = ZoneId.systemDefault();
LocalDate localDate = LocalDate.of(2020, 6, 11);
ZonedDateTime zdt = localDate.atStartOfDay(zoneId);
Date date1 = Date.from(zdt.toInstant());

LocalDateTime获取时间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
LocalDate localDate = LocalDate.now();

// 当前时间加9天
LocalDate createTime = LocalDate.now().plusDays(9);

// LocalDate比较两个时间天数差
long day=localDate.toEpochDay() - createTime.toEpochDay();
System.out.println(day);
// 获取当前月的第一天
LocalDateTime firstday = date.with(TemporalAdjusters.firstDayOfMonth());

// 获取当前月的第一天
LocalDateTime firstday = date.with(TemporalAdjusters.firstDayOfMonth());

// 当月的第一天:2021-07-01
LocalDate firstday = localDateTimeMonth.toLocalDate()
.with(ChronoField.DAY_OF_MONTH, 1L);

// 获取当前月的最后一天
LocalDateTime lastDay = date.with(TemporalAdjusters.lastDayOfMonth());

// 当天零点
LocalDateTime today_start = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);

获取当前时间,前一天,前一月,前一年

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

Date date = format.parse("2020-12-12");

// 获取当前日期
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
Date date = cal.getTime();
String nowTime = format.format(date);

// 获取前一天
cal.add(Calendar.DATE, -1);
Date date = cal.getTime();
String yesterday = format.format(date);

// 获取前一月
cal.add(Calendar.MONTH, -1);
Date date = cal.getTime();
String lastMonth = format.format(date);

// 获取前一年
cal.add(Calendar.YEAR, -1);
Date date = cal.getTime();
String lastYear = format.format(date);

原文链接:https://blog.csdn.net/qq_25391785/article/details/84934723

时间转换处理

LocalDateTime

LocalDateTime获取前、后的日子

1
2
LocalDateTime.now().minusDays(2); //两天前的日期
LocalDateTime.now().plusDays(2); //两天后的日期

LocalDateTime 转localDate

1
LocalDate localDate = LocalDateTime.now().toLocalDate();

LocalDateTime 转 Long

1
long milli = LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();

LocalDateTime 转 Date

1
Date date = Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant());

LocalDateTime 转 String

1
2
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String format = LocalDateTime.now().format(dtf);

LocalDate 转 LocalDateTime

1
2
LocalDateTime of = LocalDateTime.of(localDate, LocalTime.parse("00:00:00"));
System.err.println(of);

LocalDate 转 Long

1
2
long milli = localDate.atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli();
System.err.println(milli);

LocalDate 转 Date

1
2
Date date = Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
System.err.println(date);

LocalDate 转 String

1
2
String format = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
System.err.println(format);

Date 转 LocalDateTime

1
2
3
Date date = new Date();
LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
System.err.println(localDateTime);

Date 转 Long

1
2
3
Date date = new Date();
long time = date.getTime();
System.err.println(time);

Date 转 LocalDate

1
2
3
Date date = new Date();
LocalDate localDate = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).toLocalDate();
//先将date转为了localDateTime,随后toLocalDate

Date 转 String

1
2
3
Date date = new Date();
SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.format(date);

String 转 LocalDateTime

1
2
3
4
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String strTime = "2020-11-04 00:00:00";
LocalDateTime localDateTime = LocalDateTime.parse(strTime, dtf);
System.err.println(localDateTime);

String 转 LocalDate

1
2
3
4
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String strTime = "2020-11-04 00:00:00";
LocalDate localDate = LocalDateTime.parse(strTime, dtf).toLocalDate();
System.err.println(localDate);

String 转 Date

1
2
3
4
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String strTime = "2020-11-04 00:00:00";
Date date = Date.from(LocalDateTime.parse(strTime, dtf).atZone(ZoneId.systemDefault()).toInstant());
System.err.println(date);

Long 转 LocalDateTime

1
2
3
long milli = 1604458818000L;
LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(milli), ZoneId.systemDefault());
System.err.println(localDateTime);

Long 转 LocalDate

1
2
3
4
5
6
long milli = 1604458818000L;
LocalDate localDate = LocalDateTime.ofInstant(Instant.ofEpochMilli(milli), ZoneId.systemDefault()).toLocalDate();
System.err.println(localDate);

// 零时区UTC转上海时间
LocalDateTime localDatePro = Instant.ofEpochMilli(1627200000000L).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();

Calendar

1
2
3
4
5
6
Date date = new Date();//获取当前时间    
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.YEAR, -1);//当前时间减去一年,即一年前的时间
calendar.add(Calendar.MONTH, -1);//当前时间前去一个月,即一个月前的时间
calendar.getTime();//获取一年前的时间,或者一个月前的时间

自定义日期格式化

1
2
3
4
5
6
7
8
9
//自定义格式化 
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDateTime localDateTime = LocalDateTime.parse("2019-07-31 00:00:00", dtf);
LocalDate localDate = LocalDate.parse("2019-07-31", dtf1);
Date date = Date.from(LocalDateTime.parse("2019-07-31 00:00:00", dtf).atZone(ZoneId.systemDefault()).toInstant());
System.err.println(localDateTime);
System.err.println(localDate);
System.err.println(date);

引自:CSDN博主-一个bug玩一天

参考-Mysql查询当天,本周,本月所有数据记录

打赏
  • 版权声明: 本网站所有文章除特别声明外,均采用 Apache License 2.0 许可协议。转载请注明出处!
  1. © 2020-2021 Lauy    湘ICP备20003709号-1

请我喝杯咖啡吧~

支付宝
微信