1. 程式人生 > >java 獲取 上個月的今天

java 獲取 上個月的今天

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日");
Date date = new Date();

System.out.println("當前時間是:" + dateFormat.format(date));

Calendar calendar = Calendar.getInstance();
calendar.setTime(date); // 設定為當前時間
calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1); // 設定為上一個月
date = calendar.getTime
(); System.out.println("上一個月的時間: " + dateFormat.format(date));