JAVA:時間的處理
RojerChen.2017.09.12
近期在跟 JAVA 奮戰,由於久久才寫一次,有很多語法寫了就忘,忘了又查,還是整理一下好了。
日期轉字串
public static String DateToString(Date date){ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return format.format(date); }
字串轉日期
日期時間差
日期時間差
public static Date StringToDate(String dateString){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return format.parse(dateString,new ParsePosition(1));
}
日期時間差
public static double DateDiffHour(Date dateStart,Date dateEnd,Integer second,Integer roundN){
long number1 = dateEnd.getTime() - dateStart.getTime();
long number2 = second * 1000;
BigDecimal b1 = new BigDecimal(number1);
BigDecimal b2 = new BigDecimal(number2);
return b1.divide(b2,roundN,BigDecimal.ROUND_HALF_UP).doubleValue();
}
日期時間差
Date start = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date end = new Date();
long diff = end.getTime()-start.getTime();
System.out.println(diff);
目前時間
private String GetTime(){
SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss.SSS");
Calendar now = Calendar.getInstance();
timeFormat.setTimeZone(TimeZone.getTimeZone("GMT+8"));
return timeFormat.format(now.getTime());
}
0 意見:
張貼留言