日期轉化時間戳,時間戳轉化日期
public class Test{ public void test() { String stime = "2016-12-01 11:22:00"; String timeStamp2 = date2TimeStamp(stime, "yyyy-MM-dd HH:mm:ss"); System.out.println(timeStamp2); int times = Integer.parseInt(timeStamp2)+1; String date = timeStamp2Date(String.valueOf(times), "yyyy-MM-dd HH:mm:ss"); System.out.println(date); } public static String date2TimeStamp(String date_str,String format){ try { SimpleDateFormat sdf = new SimpleDateFormat(format); return String.valueOf(sdf.parse(date_str).getTime()/1000); } catch (Exception e) { e.printStackTrace(); } return ""; } public static String timeStamp2Date(String seconds,String format) { if(seconds == null || seconds.isEmpty() || seconds.equals("null")){ return ""; } if(format == null || format.isEmpty()) format = "yyyy-MM-dd HH:mm:ss"; SimpleDateFormat sdf = new SimpleDateFormat(format); return sdf.format(new Date(Long.valueOf(seconds+"000"))); } public static void main(String[] args) { Test test1 = new Test(); test1 .test(); } }