Date dNow = new Date (); // current time
Date dBefore = new Date ();
Calendar calendar = Calendar. getInstance (); // obtain the Calendar
Calendar. setTime (dNow); // assign the current time to the calendar
Calendar. add (Calendar. DAY_OF_MONTH,-1); // set it to the previous day.
DBefore = calendar. getTime (); // obtain the time of the previous day.
SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"); // set the time format
String defaultStartDate = sdf. format (dBefore); // format the previous day
String defaultEndDate = sdf. format (dNow); // format the current time
System. out. println ("the previous day is:" + defaultStartDate );
System. out. println ("generated at:" + defaendenddate );
In addition:
Conversion Between Calendar and Date:
Calendar calendar = Calendar. getInstance ();
// Obtain the Date object from a Calendar Object
Date date = calendar. getTime ();
// Returns the Date object to a Calendar Object,
// Obtain an instance and set the Date object
Calendar. setTime (date );
The starting value of the month is 0 rather than 1. Therefore, we need to set 7 instead of 8 for January 1, August.
Calendar. set (Calendar. MONTH, 7 );
Calculate the day of the week from 2000-01-01.
Calendar. get (calendar. DAY_OF_WEEK)
Java code
Import Java. util .*;
Public class ShowDate {
Public static void main (String [] args ){
Calendar calendar = new GregorianCalendar ();
Date trialTime = new Date ();
Calendar. setTime (trialTime );
// Print out a bunch of interesting things
System. out. println ("ERA:" + calendar. get (Calendar. ERA ));
System. out. println ("YEAR:" + calendar. get (Calendar. YEAR ));
System. out. println ("MONTH:" + calendar. get (Calendar. MONTH ));
System. out. println ("WEEK_OF_YEAR:" + calendar. get (Calendar. WEEK_OF_YEAR ));
System. out. println ("WEEK_OF_MONTH:" + calendar. get (Calendar. WEEK_OF_MONTH ));
System. out. println ("DATE:" + calendar. get (Calendar. DATE ));
System. out. println ("DAY_OF_MONTH:" + calendar. get (Calendar. DAY_OF_MONTH ));
System. out. println ("DAY_OF_YEAR:" + calendar. get (Calendar. DAY_OF_YEAR ));
System. out. println ("DAY_OF_WEEK:" + calendar. get (Calendar. DAY_OF_WEEK ));
System. out. println ("DAY_OF_WEEK_IN_MONTH:" + calendar. get (Calendar. DAY_OF_WEEK_IN_MONTH ));
System. out. println ("AM_PM:" + calendar. get (Calendar. AM_PM ));
System. out. println ("HOUR:" + calendar. get (Calendar. HOUR ));
System. out. println ("HOUR_OF_DAY:" + calendar. get (Calendar. HOUR_OF_DAY ));
System. out. println ("MINUTE:" + calendar. get (Calendar. MINUTE ));
System. out. println ("SECOND:" + calendar. get (Calendar. SECOND ));
System. out. println ("MILLISECOND:" + calendar. get (Calendar. MILLISECOND ));
System. out. println ("ZONE_OFFSET:" + (calendar. get (Calendar. ZONE_OFFSET)/(60*60*1000 )));
System. out. println ("DST_OFFSET:" + (calendar. get (Calendar. DST_OFFSET)/(60*60*1000 )));
System. out. println ("Current Time, with hour reset to 3 ");
Calendar. clear (Calendar. HOUR_OF_DAY); // so doesn't override
Calendar. set (Calendar. HOUR, 3 );
System. out. println ("ERA:" + calendar. get (Calendar. ERA ));
System. out. println ("YEAR:" + calendar. get (Calendar. YEAR ));
System. out. println ("MONTH:" + calendar. get (Calendar. MONTH ));
System. out. println ("WEEK_OF_YEAR:" + calendar. get (Calendar. WEEK_OF_YEAR ));
System. out. println ("WEEK_OF_MONTH:" + calendar. get (Calendar. WEEK_OF_MONTH ));
System. out. println ("DATE:" + calendar. get (Calendar. DATE ));
System. out. println ("DAY_OF_MONTH:" + calendar. get (Calendar. DAY_OF_MONTH ));
System. out. println ("DAY_OF_YEAR:" + calendar. get (Calendar. DAY_OF_YEAR ));
System. out. println ("DAY_OF_WEEK:" + calendar. get (Calendar. DAY_OF_WEEK ));
System. out. println ("DAY_OF_WEEK_IN_MONTH:" + calendar. get (Calendar. DAY_OF_WEEK_IN_MONTH ));