Java8 LocalDateTime 相关时间格式转换

  • Post author:
  • Post category:java



Java8

1、LocalDateTime获取时间戳(毫秒/秒)、

2、Date与LocalDateTime互转

3、LocalDateTime与自定义的时间格式的字符串String互转

4、long类型的timestamp与LocalDateTime互转




1、LocalDateTime获取毫秒数

//获取秒数
Long second = LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));

//获取毫秒数
Long milliSecond = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();



2、Date与LocalDateTime互转

    //将java.util.Date 转换为java8 的java.time.LocalDateTime,默认时区为东8区
    public static LocalDateTime dateConvertToLocalDateTime(Date date) {
        return date.toInstant().atOffset(ZoneOffset.of("+8")).toLocalDateTime();
    }

   
    //将java8 的 java.time.LocalDateTime 转换为 java.util.Date,默认时区为东8区
    public static Date localDateTimeConvertToDate(LocalDateTime localDateTime) {
        return Date.from(localDateTime.toInstant(ZoneOffset.of("+8")));
    }



3.将LocalDateTime转为自定义的时间格式的字符串

public static String getDateTimeAsString(LocalDateTime localDateTime, String format) {
  DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
  return localDateTime.format(formatter);
}



4.将某时间字符串转为自定义时间格式的LocalDateTime

public static LocalDateTime parseStringToDateTime(String time, String format) {
  DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
  return LocalDateTime.parse(time, df);
}



5.将long类型的timestamp转为LocalDateTime

public static LocalDateTime getDateTimeOfTimestamp(long timestamp) {
  Instant instant = Instant.ofEpochMilli(timestamp);
  ZoneId zone = ZoneId.systemDefault();
  return LocalDateTime.ofInstant(instant, zone);
}



6.将LocalDateTime转为long类型的timestamp

public static long getTimestampOfDateTime(LocalDateTime localDateTime) {
  ZoneId zone = ZoneId.systemDefault();
  Instant instant = localDateTime.atZone(zone).toInstant();
  return instant.toEpochMilli();
}



时间工具类:DateUtil.java


public class DateUtil {
	private static final long ONE_DAY=24*60*60*1000;
	
	private static final String DATE_FORMAT1="yyyy-MM-dd";
	private static final String DATE_FORMAT2="yyyyMMdd";
	private static final String DATETIME_FORMAT1="yyyy-MM-dd HH:mm:ss";
	private static final String DATETIME_FORMAT2="yyyyMMddHHmmss";
	private static final String DATETIME_FORMAT3="yyyy-MM-dd HH:mm:ss.SSS";
	private static final String DATETIME_FORMAT4="yyyyMMddHHmmssSSS";

	private static final String TIME_FORMAT1="HH:mm:ss";
	
	

	//获取当前时间(字符串格式)
	public static getCurrentDateTime(String format){
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
		return LocalDateTime.now().format(formatter);
	}
	
    //获取昨天时间格式
	public static getYesterdayByFormat(String format){
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
		return LocalDateTime.now().minusDays(1).format(formatter);
	}

	//获取从1970年1月1日到现在的秒数
	public static Long getSecond(String dateTime,String format){
		DateTimeFormatter dtf = DateTimeFormatter.ofPattern(format);
		LocalDateTime localDateTime = LocalDateTime.parse(dateTime,dtf);
		Long second = localDateTime.toEpochSecond(ZoneOffset.of("+8"));
		return second;
	}

	//LocalDateTime转String
	public static String getLocalDateTimeToStr(LocalDateTime localDateTime, String format){
		DateTimeFormatter dtf = DateTimeFormatter.ofPattern(format);
		return localDateTime.format(dtf);
	}

	//获取从1970年1月1日到现在的秒数转LocalDateTime
	public static LocalDateTime getSecondToLocalDateTime(Long second) {
		Instant instant = Instant.ofEpochSecond(second);
		ZoneId zoneId = ZoneId.systemDefault();
		return LocalDateTime.ofInstant(instant, zoneId);
	}

	//获取从1970年1月1日到现在的秒数转字符串格式
    public static String getSecondToStr(long second, String format){
		return getLocalDateTimeToStr(getSecondToLocalDateTime(second),format)
	}
}



版权声明:本文为weixin_40434637原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。