Java obtains the previous day of the current time and converts between the Calendar AR and Date.

Source: Internet
Author: User


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 ));


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.