Java about getting the current time and the usage of yesterday's time __java

Source: Internet
Author: User
Tags time and date

I. Get the current system time and date and format the output:

Import Java.util.Date;
Import Java.text.SimpleDateFormat;

public class Nowstring {
public static void Main (string[] args) {
SimpleDateFormat df = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");//Set Date format
System.out.println (Df.format (new date));//new Date () to get current system time
}
}

Ii. acquisition of yesterday's time

Take yesterday's date, I would like to cut out the date of a good. Another thought wrong, if today is a number how to do.

Two options available

1:date as = new Date (new Date (). GetTime () -24*60*60*1000);
SimpleDateFormat matter1 = new SimpleDateFormat ("Yyyy-mm-dd");
String time = Matter1.format (AS);
System.out.println (time);

Take out the digital time and subtract 24*60*60*1000, you get yesterday's time;

This is a little out of date.

2:calendar cal = Calendar.getinstance ();
Cal.add (Calendar.date,-1);
String yesterday = new SimpleDateFormat ("Yyyy-mm-dd"). Format (Cal.gettime ());
System.out.println (yesterday);

This method is very convenient, the month and the day can change as one pleases. Look at the program below.

Package Com.yiibai;
Import Java.util.Calendar;

public class Calendardemo {public

   static void Main (string[] args) {
   
      //Create a calendar
      Calendar cal = Calen Dar.getinstance ()
	  
      //print current date
      System.out.println ("The current date is:" + cal.gettime ());

      Add
      to the Calendar cal.add (calendar.date);
      System.out.println ("Days later:" + cal.gettime ());

      Subtract 2 months
      from the calendar cal.add (Calendar.month,-2);
      System.out.println ("2 months ago:" + cal.gettime ());

      Subtract 5 year
      from the calendar cal.add (Calendar.year,-5);
      System.out.println ("5 years ago:" + cal.gettime ());
   }

Let's compile and run the above program, which will produce the following results:

The current date Is:thu Apr 02:13:28 eest later:wed may
02:13:28 eest
2 months Ago:fri Mar 02:13:28 EET
5 years ago:fri Mar 02:13:28 EET 2007




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.