Java日期加減

public static String getDate(String dateGiven,Integer day) throws Exception{
    SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
    Date date=sdf.parse(dateGiven);
    Calendar calendar = new GregorianCalendar();
    calendar.setTime(date);
    calendar.add(Calendar.DATE,day);//把日期日後增長一天.整數日後推,負數往前移動
    date=calendar.getTime(); //這個時間就是日期日後推一天的結果
    String dateString = formatter.format(date);
    return dateString;
}

----------------------------------------------華麗的分割線------------------------------------------------spa

// 如下整理來自 「加菲愛」 點提基於Java8的實用方法

// 當前系統日期
LocalDateTime now = LocalDateTime.now();
System.out.println("當前系統時間爲:" + now);
// 一天後的日期
System.out.println("一天後的日期爲:" + now.plusDays(1L));
// 一天前的日期
System.out.println("一天前的日期爲:" + now.minusDays(1L));

// LocalDateTime 格式化時間
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formatNow = dtf.format(now);
System.out.println("格式化當前系統時間爲:" + formatNow);
相關文章
相關標籤/搜索