1、时间转时间戳
public static long strToTimestamp(String dateTimeStr) throws Exception {
Timestamp time = Timestamp.valueOf(dateTimeStr);
return time.getTime();
}
2、时间戳转时间
public static String timestampToStr(long timestamp) throws Exception {
Timestamp ts = new Timestamp(timestamp);
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(ts);
}
3、时间转换
public static Map strTimeTomap(String dateTimeStr) throws Exception {
// Timestamp ts = new Timestamp(timestamp);
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
DateFormat dsdf = new SimpleDateFormat("yyyy-MM-dd"); Calendar cal = Calendar.getInstance(); Date date = sdf.parse(dateTimeStr);
cal.setTime(date);
String week_day = String.valueOf(cal.get(Calendar.DAY_OF_WEEK) - 1);
String hour = String.valueOf(cal.get(Calendar.HOUR_OF_DAY)); cal.set(Calendar.DAY_OF_MONTH, 1);
String first_day_month = dsdf.format(cal.getTime());
// System.out.println(first_day_month); cal.setTime(date);
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
String first_day_week = dsdf.format(cal.getTime());
// System.out.println(first_day_week); String day = dsdf.format(date);
Map map = new HashMap<String,String>();
map.put("hour",hour);
map.put("day",day);
map.put("week_day",week_day);
map.put("first_day_week",first_day_week);
map.put("first_day_month",first_day_month); return map;
} @Test
public void test() throws Exception {
System.out.println(strTimeTomap("2019-04-18 12:31:05"));
}