我刚写完这个代码,就被开除了........
代码双手奉上以备后用:
package mydemo; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; public class ThreeDay { public static void main(String[] args) { getToday(); getYesterday(); getTomorrow(); } /** * 获取今天的日期 */ public static void getToday() { //SimpleDateFormat smdate = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); SimpleDateFormat smdate = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); String date = smdate.format(new Date()); System.out.println(date); } /** * 获取昨天的日期 */ public static void getYesterday() { SimpleDateFormat smdate = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); Calendar calendar = new GregorianCalendar(); calendar.setTime(new Date()); calendar.add(calendar.DATE, -1); String date = smdate.format(calendar.getTime()); System.out.println(date); } /** * 获取明天的日期 */ public static void getTomorrow() { SimpleDateFormat smdate = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); Calendar calendar = new GregorianCalendar(); calendar.setTime(new Date()); calendar.add(calendar.DATE, 1); String date = smdate.format(calendar.getTime()); System.out.println(date); } }
结果:
原文地址:https://www.cnblogs.com/xiejn/p/12037954.html
时间: 2024-10-01 21:06:53