DateUtils.java 1.2KB

12345678910111213141516171819202122232425262728293031323334353637
  1. package com.yunzhi.demo.common;
  2. import java.time.*;
  3. import java.time.format.DateTimeFormatter;
  4. public class DateUtils {
  5. public static LocalDateTime from(String str, String formater) {
  6. DateTimeFormatter fmt = DateTimeFormatter.ofPattern(formater);
  7. return LocalDateTime.parse(str, fmt);
  8. }
  9. public static long daysBetween(LocalDateTime dt1, LocalDateTime dt2) {
  10. Duration duration = Duration.between(dt1, dt2);
  11. return duration.toDays();
  12. }
  13. public static String toString(LocalDateTime dt, String formater) {
  14. DateTimeFormatter formatter = DateTimeFormatter.ofPattern(formater);
  15. return dt.format(formatter);
  16. }
  17. public static String weekDay(LocalDateTime dt) {
  18. String[] week = new String[]{"一", "二", "三", "四", "五", "六", "日"};
  19. DayOfWeek dayOfWeek = dt.getDayOfWeek();
  20. int inx = dayOfWeek.getValue() - 1;
  21. return String.format("星期%s", week[inx]);
  22. }
  23. /**
  24. * 毫秒转 UTC 时间
  25. * @param milliseconds
  26. * @return
  27. */
  28. public static LocalDateTime getDateTime(long milliseconds) {
  29. return LocalDateTime.ofInstant(Instant.ofEpochMilli(milliseconds), ZoneOffset.UTC);
  30. }
  31. }