You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
3.7 KiB

3 years ago
package com.qs.serve.common.util;
import com.qs.serve.common.util.model.DateFormatString;
import lombok.experimental.UtilityClass;
import java.text.ParseException;
import java.text.SimpleDateFormat;
2 years ago
import java.time.*;
3 years ago
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;
/**
* @author YenHex
* @since 2022/3/1
*/
@UtilityClass
public class DateUtils {
public static Date localDateToDate(LocalDate date){
//LocalDate date = LocalDate.of(2006,07,26);
ZoneId zone = ZoneId.systemDefault();
Instant instant = date.atStartOfDay().atZone(zone).toInstant();
java.util.Date da = Date.from(instant);
return da;
}
3 years ago
public static String getString(DateFormatString format){
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format.getValue());
return LocalDateTime.now().format(formatter);
}
2 years ago
public static LocalDate toLocalDate(Date date){
// Instant instant = date.toInstant();
// ZoneId zoneId = ZoneId.systemDefault();
// return instant.atZone(zoneId).toLocalDate();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String str = dateFormat.format(date);
String[] a = str.split("-");
return LocalDate.of(Integer.parseInt(a[0]),Integer.parseInt(a[1]),Integer.parseInt(a[2]));
}
public static Date toDate(LocalDate localDate){
ZoneId zoneId = ZoneId.systemDefault();
ZonedDateTime zdt = localDate.atStartOfDay(zoneId);
return Date.from(zdt.toInstant());
}
3 years ago
/**
* 获取设置后得时间
* @param time
* @param unit
* @param num
* @return
*/
public static LocalDateTime beSetTime(LocalDateTime time, ChronoUnit unit,int num){
if(num>0){
return time.plus(num,unit);
}else {
return time.minus(-num,unit);
}
}
public static LocalDate beSetDate(LocalDate time,int days){
return time.plusDays(days);
}
public static Date toDate(String str,String format){
SimpleDateFormat sdf = new SimpleDateFormat(format);
try {
return sdf.parse(str);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
public static Date toDate(LocalDateTime time){
return Date.from( time.atZone( ZoneId.systemDefault()).toInstant());
}
public static double diffTime(long beganTime, long endTime) {
return (double)(endTime - beganTime) / 1000.0D;
}
public static String format(LocalDateTime localDateTime, DateFormatString format){
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format.getValue());
return localDateTime.format(formatter);
}
public static String datePath(){
return format(LocalDate.now(),DateFormatString.DATE);
}
public static String format(LocalDate localDate, DateFormatString format){
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format.getValue());
return localDate.format(formatter);
}
public static void main(String[] args) {
System.out.println(DateUtils.beSetTime(LocalDateTime.now(), ChronoUnit.DAYS,-2));
// Get the LocalDateTime instance
LocalDateTime ldt
= LocalDateTime
.parse("2019-12-31T19:15:30");
// Get the String representation of this LocalDateTime
System.out.println("Original LocalDateTime: "
+ ldt.toString());
// subtract 200 DAYS to LocalDateTime
LocalDateTime value
= ldt.minus(200, ChronoUnit.DAYS);
// print result
System.out.println("LocalDateTime after subtracting DAYS: "
+ value);
}
}