关于LocalDateTime最常用方法和时间转换方式
目录
- LocalDateTime最常用方法和时间转换
- 字符串转LocalDateTime
- LocalDateTime转字符串
- Long型时间戳(15位)转UTC时间字符串
- Long型时间戳(15位)转UTC时间字符串
- 获取今天的起止时间
- 返回目标月份的最后一天
- 获取某天的起止时间(优雅)
- LocalDateTime设置自定义时间
- Long毫秒时间戳转LocalDateTime
- 根据LocalDateTime获取毫秒时间戳
- Long时间戳根据时区转为当地日期
- LocalDateTime转Date
- Date转LocalDateTime
- 毫秒转秒(时间戳)
- 秒转毫秒(时间戳)
- 总结
LocalDateTime最常用方法和时间转换
Java 8 版本以后,新增了了LocalDateTime和了LocalDate类,转换方便不亚于jodaTIme。
LocalDateTime方法有很多,本文将开发中最常用的一些时间转换列举出来,并给出转换后的示例,希望大家可以转换时参考使用。
字符串转LocalDateTime
public static LocalDateTime parseStringToDateTime(String time, String format) { DateTimeFormatter df = DateTimeFormatter.ofPattern(format); return LocalDateTime.parse(tandroidime, df); }
LocalDateTime转字符串
public static String getDateTimeToString(LocalDateTime localDateTime, String format) 编程客栈{ DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format); return localDateTime.format(formatter); }
测试:
若毫秒位不为000则显示毫秒
String utc = getUTCMillisecondFromTimestamp(1539718265123); System.out.println(utc); 输出:2018-10-16T19:31:05.123Z
若毫秒为000则不显示毫秒,
String utc = getUTCMillisecondFromTimestamp(1539718265000); System.out.println(utc); 输出:2018-10-16T19:31:05Z
Long型时间戳(15位)转UTC时间字符串
/** * 将UTC时间戳转为UTC字符串(精确到秒) * * @param timestamp * @return */ public static String getUTCStrFromTimestamp(long timestamp) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'"); // 北京时间:ZoneId.of("UTC+8");系统默认时间:ZoneId.systemDefault() LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.of("UTC")); String utc =www.devze.com localDateTime.format(formatter); return utc; }
测试:
String utc = getUTCStrFromTimestamp(1539718265123); System.out.println(utc); 输出:2018-10-16T19:31:05Z
Long型时间戳(15位)转UTC时间字符串
/** * 将时间戳转为UTC字符串(若毫秒为000则不显示毫秒,若毫秒位不为000则显示毫秒) * * 返回格式:2018-10-16T19:31:05.123Z * * @param timestamp * @return */ public static String getUTCMillisecondFromTimestamp(long timestamp) { Instant instant = Instant.ofEpochMilli(timestamp); String utcString = instant.toString(); return utcString; }
获取今天的起止时间
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'"); String startTime = LocalDateTime.now().withHour(0).withMinute(0).withSecond(0).withNano(0).format(formatter)js; String endTime编程客栈 = LocalDateTime.now().withHour(23).withMinute(59).withSecond(59).withNano(999_999_999).format(formatter);
返回目标月份的最后一天
/** * 返回目标月份的最后一天 * * @param sourceDate 目标月份 * @param monthOffset 月份偏移量 * @return */ private LocalDate getLastDateOfMonth(LocalDate sourceDate, int monthOffset) { return sourceDate.plusMonths(monthOffset).with(TemporalAdjusters.lastDayOfMonth()); }
获取某天的起止时间(优雅)
LocalDate date = LocalDate.of(2021, 4, 20); LocalDateTime startTime = LocalDateTime.of(date, LocalTime.MIN); LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
LocalDateTime设置自定义时间
/** * 获得一个(毫秒)时间戳的十分钟起始时间戳 * * 如:输入1534930875000(2018-08-22 17:41:15)的时间戳返回1534930800000(2018-08-22 17:40:00)的时间戳 * * @param timestrap * * * @return */ public static long getTenMinuteStartTimestrap(long timestamp) { LocalDateTime localDateTime = getLocalDateTimeByTimestamp(timestamp); int minute = localDateTime.getMinute(); // 分钟个位数置为0 int minute_floor = minute / 10 * 10; LocalDateTime startLocalDateTime = localDateTime.withMinute(minute_floor).withSecond(0).withNano(0); long startTimestamp = getTimestampByLocalDateTime(startLocalDateTime); return startTimestamp; }
Long毫秒时间戳转LocalDateTime
/** * Long毫秒时间戳转LocalDateTime */ public static LocalDateTime getLocalDateTimeByTimestamp(long timestamp) { Instant instant = Instant.ofEpochMilli(timestamp); LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneOffset.UTC); return localDateTime; }
根据LocalDateTime获取毫秒时间戳
/** * 根据毫秒时间戳获取LocalDateTime * @param localDateTime * @return */ public static long getTimestampByLocalDateTime(LocalDateTime localDateTime) { long timestamp = localDateTime.atZone(ZoneId.of("UTC")).toInstant().toEpochMilli(); return timestamp; }
Long时间戳根据时区转为当地日期
/** * 时间戳根据时区转为当地日期 * @param timestamp * @param timezone * @return */ public static Date getLocalDateByTimezone(long timestamp, int timezone) { Instant instant = Instant.ofEpochMilli(timestamp); LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneOffset.UTC); // 当地时间 LocalDateTime localDate = localDateTime.plusHours(timezone); DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'"); String localDateTimeStr = df.format(localDate); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); Date date = null; try { date = sdf.parse(localDateTimeStr); } catch (ParseException e) { e.printStackTrace(); } return date; }
LocalDateTime转Date
注意:系统所在时区
Date date = Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant());
Date转LocalDateTime
Date todayDate = new Date(); LocalDateTime ldt = todayDate.toInstant() .atZone( ZoneId.systemDefault() ) .toLocalDateTime();
毫秒转秒(时间戳)
long second = Instant.ofEpochMilli(1636338645000L).getEpochSecond();
秒转毫秒(时间戳)
long second = Instant.ofEpochSecond(1636338645L).toEpochMilli();
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程客栈(www.devze.com)。
精彩评论