TimeUtils.java 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package com.lqkj.common.utils;
  2. import java.text.ParseException;
  3. import java.text.SimpleDateFormat;
  4. import java.util.Date;
  5. public class TimeUtils {
  6. /**
  7. * 时间戳转字符串
  8. *
  9. * @param time 时间戳
  10. * @param format 格式,如"yyyy-MM-dd HH:mm:ss"
  11. * @return 时间格式字符串
  12. */
  13. public static String getTimeString(long time, String format) {
  14. if (format == null || format.isEmpty())
  15. format = "yyyy-MM-dd HH:mm:ss";
  16. SimpleDateFormat sdf = new SimpleDateFormat(format);
  17. return sdf.format(new Date(time));
  18. }
  19. public static Long getTimestamp(String time) {
  20. return getTimestamp(time, "yyyy-MM-dd HH:mm:ss");
  21. }
  22. /**
  23. * 字符串转时间戳
  24. *
  25. * @param time 字符串
  26. * @return 时间戳
  27. **/
  28. public static Long getTimestamp(String time, String format) {
  29. if (format == null || format.isEmpty())
  30. format = "yyyy-MM-dd HH:mm:ss";
  31. Long timestamp = null;
  32. try {
  33. timestamp = new SimpleDateFormat(format).parse(time).getTime();
  34. } catch (ParseException e) {
  35. e.printStackTrace();
  36. }
  37. return timestamp;
  38. }
  39. }