package com.lqkj.common.utils; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class TimeUtils { /** * 时间戳转字符串 * * @param time 时间戳 * @param format 格式,如"yyyy-MM-dd HH:mm:ss" * @return 时间格式字符串 */ public static String getTimeString(long time, String format) { if (format == null || format.isEmpty()) format = "yyyy-MM-dd HH:mm:ss"; SimpleDateFormat sdf = new SimpleDateFormat(format); return sdf.format(new Date(time)); } public static Long getTimestamp(String time) { return getTimestamp(time, "yyyy-MM-dd HH:mm:ss"); } /** * 字符串转时间戳 * * @param time 字符串 * @return 时间戳 **/ public static Long getTimestamp(String time, String format) { if (format == null || format.isEmpty()) format = "yyyy-MM-dd HH:mm:ss"; Long timestamp = null; try { timestamp = new SimpleDateFormat(format).parse(time).getTime(); } catch (ParseException e) { e.printStackTrace(); } return timestamp; } }