Java Common date format conversions and date acquisition

Source: Internet
Author: User

Package Com.test.TestBoot.SingleModel;
import Java.text.SimpleDateFormat;
import java.util.Date;
Public class Test {
Public static void Main (string[] args) {
 
        /**
* Date Turn String
         */
Date date = new Date ();
String datestr = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"). Format (date);
System.out.println (DATESTR);
        
        
        /**
*string Turn Date
         */
String ss = "2016-10-24 21:59:06";
SimpleDateFormat sdf = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
try {
Date D = sdf.parse (ss);
System.out.println (d);
} catch (ParseException e) {
e.printstacktrace ();
        }

/**
* First day of the month
*/
Calendar MONTHCA = Calendar.getinstance ();
Monthca.add (calendar.month, 0);
Monthca.set (calendar.day_of_month,1);
String monthfirstday = Sdf.format (Monthca.gettime ()); First day of the month
System.out.println (Monthfirstday);

/**
* Last day of the month
*/
Calendar calendar = Calendar.getinstance ();
Calendar.set (Calendar.date, Calendar.getactualmaximum (calendar.date));
String monthlastday = Sdf.format (Calendar.gettime ()); Last day of the month
System.out.println (Monthlastday);

/**
* Yesterday
*/
Calendar STARTCA = Calendar.getinstance (); Get an example of a calendar
Startca.settime (New Date ()); Set time to current time
Startca.add (Calendar.date,-1); Date minus 1
Date sdate = Startca.gettime (); The day before
SimpleDateFormat sd = new SimpleDateFormat ("Yyyy-mm-dd");
String starttime = Sd.format (sdate); The day before (yesterday)
System.out.println (starttime);

/**
* First day of last month
*/
Calendar Lastmonthfirst = Calendar.getinstance ();
Lastmonthfirst.add (Calendar.month,-1);
Lastmonthfirst.set (Calendar.day_of_month, 1);
String lastmonthfirstday = Sd.format (Lastmonthfirst.gettime ()); The first day of last month
System.out.println (Lastmonthfirstday);

/**
* The last day of last month
*/
Calendar lastmonthlast = Calendar.getinstance ();
Lastmonthlast.set (Calendar.day_of_month, 1);
Lastmonthlast.add (Calendar.date,-1);
String lastmonthlastday = Sd.format (Lastmonthlast.gettime ()); Last day of last month
System.out.println (Lastmonthlastday);
         
    }
   
}

Java Common date format conversions and date acquisition

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.