Java get current time and day before date (implementation code) _java

Source: Internet
Author: User
Tags set time

Nonsense not much to say that on the code

Copy Code code as follows:

String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";

Date Dnow = new Date (); Current time
Date Dbefore = new Date ();

Calendar calendar = Calendar.getinstance (); Get the Calendar
Calendar.settime (Dnow)//Assign the current time to the calendar
Calendar.add (Calendar.day_of_month,-1); Set to the day before
Dbefore = Calendar.gettime (); Get the day before


SimpleDateFormat sdf=new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); Set time format
String defaultstartdate = Sdf.format (Dbefore); Format the day before
String defaultenddate = Sdf.format (Dnow); Format the current time


System.out.println ("The day before is:" + defaultstartdate);

System.out.println ("Generated time is:" + defaultenddate);

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.