Java8新的日期Api,java.time包下的所有类都是不可变类型而且线程安全。
Date互转LocalDate
以下很多都是demo
import org.apache.commons.lang3.time.DateFormatUtils;
import java.lang.management.ManagementFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;
public class DateUtils
{
public static String YYYY = "yyyy";
public static String YYYY_MM = "yyyy-MM";
public static String YYYY_MM_DD = "yyyy-MM-dd";
public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
public static String getNowDay(){
LocalDateTime now = LocalDateTime.now();
return now.format(DateTimeFormatter.ofPattern(YYYY_MM_DD_HH_MM_SS));
}
public static LocalDate handleSpeciDate(){
LocalDate dateBirth = LocalDate.of(2020, 02, 12);
int year = dateBirth.getYear();
String month = dateBirth.getMonthValue() < 10 ? "0" + dateBirth.getMonthValue() : "" + dateBirth.getMonthValue();
int dayOfMonth = dateBirth.getDayOfMonth();
System.out.println(month);
return dateBirth;
}
public static LocalDate plusDayOrMinusDay(){
LocalDate now = LocalDate.now();
LocalDate localDate1 = now.plusDays(7);
System.out.println("{localDate1},{}"+ localDate1 );
LocalDate localDate2 = now.plus(1, ChronoUnit.MONTHS);
System.out.println("{localDate1},{}"+ localDate2 );
LocalDate localDate3 = now.minus(1, ChronoUnit.MONTHS);
System.out.println("{localDate1},{}"+ localDate3 );
LocalDate localDate4 = now.plusYears(1);
System.out.println("{localDate1},{}"+ localDate4 );
return localDate1;
}
public static boolean compareLocalDate(){
LocalDate now = LocalDate.now();
LocalDate dayDate = LocalDate.of(2020, 10, 20);
if (now.equals(dayDate)){
System.out.println("equals :" + now.equals(dayDate));
System.out.println(now + " $$" + dayDate);
}
if (dayDate.isAfter(dayDate)){
System.out.println("isAfter " + dayDate.isAfter(dayDate));
}
if (dayDate.isEqual(dayDate)){
System.out.println("isEqual " + dayDate.isEqual(dayDate));
}
return now.equals(dayDate);
}
public static YearMonth getYearMonth() {
YearMonth currentYearMonth = YearMonth.now();
System.out.printf("Days in yearMonth %s,month day %d%n", currentYearMonth, currentYearMonth.lengthOfMonth());
YearMonth creditCardExpiry = YearMonth.of(2028, Month.FEBRUARY);
System.out.printf("Your credit card expires on %s %n", creditCardExpiry);
return creditCardExpiry;
}
public static LocalDate date2LocalDate(Date date) {
if(null == date) {
return null;
}
return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
}
public static Date localDate2Date(LocalDate localDate) {
if(null == localDate) {
return null;
}
ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault());
return Date.from(zonedDateTime.toInstant());
}
public static void main(String[] args) {
dateToStr();
}
public static LocalDate strToDate(){
LocalDate parse = LocalDate.parse("2020-02-31", DateTimeFormatter.ofPattern(YYYY_MM_DD));
return parse;
}
public static String dateToStr(){
LocalDate parse = LocalDate.parse("2020-02-31", DateTimeFormatter.ofPattern(YYYY_MM_DD));
LocalDateTime now = LocalDateTime.now();
String format = now.format(DateTimeFormatter.ofPattern(YYYY_MM_DD_HH_MM_SS));
System.out.println(format);
return format;
}
public static Date getServerStartDate()
{
long time = ManagementFactory.getRuntimeMXBean().getStartTime();
return new Date(time);
}
public static String getDatePoor(Date endDate, Date nowDate)
{
long nd = 1000 * 24 * 60 * 60;
long nh = 1000 * 60 * 60;
long nm = 1000 * 60;
// long ns = 1000;
// 获得两个时间的毫秒时间差异
long diff = endDate.getTime() - nowDate.getTime();
// 计算差多少天
long day = diff / nd;
// 计算差多少小时
long hour = diff % nd / nh;
// 计算差多少分钟
long min = diff % nd % nh / nm;
// 计算差多少秒//输出结果
// long sec = diff % nd % nh % nm / ns;
return day + "天" + hour + "小时" + min + "分钟";
}
}