Java acquisition time year, month, day method _java

Source: Internet
Author: User

This article illustrates how Java gets time, months, and days. Share to everyone for your reference. The implementation method is as follows:

Package Com.date.demo; 
Import java.text.ParseException; 
Import Java.text.SimpleDateFormat; 
Import Java.util.Calendar; 
Import Java.util.Date; public class Datedemo {public static void main (String args[]) {System.out.println ("---------------to get the date of the current time---- 
    -------------"); 
    Getmonthday (); 
    System.out.println ("---------------to obtain the date of the custom Time-----------------"); 
  Getmonthday2set (); 
    /** * Gets the custom time month/day/private static void Getmonthday2set () {String datestr = "2013-11-10 18:45:39"; 
    SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); 
    Calendar cal = Calendar.getinstance (); 
    Date dt = null; 
      try {dt = Sdf.parse (DATESTR); 
    Cal.settime (DT); 
    catch (ParseException e) {//TODO auto-generated catch block E.printstacktrace (); 
    int year = Cal.get (calendar.year); 
    int month = Cal.get (calendar.month) + 1; 
    int day = Cal.get (calendar.day_of_month); int hour = Cal.get (Calendar.hour_of_day); 
    int minute = Cal.get (Calendar.minute); 
    int second = Cal.get (Calendar.second); 
    System.out.println ("= = = = = = =" + year); 
    System.out.println ("= = = Month = = =" + month); 
    System.out.println ("= = = Day = = =" + days); 
    System.out.println ("= = = = When = =" + hour); 
    System.out.println ("= = = = = = = = =" + minute); 
  System.out.println ("= = = Second = = =" + second); 
    /** * Gets the current time year/month/private static void Getmonthday () {Calendar cal = Calendar.getinstance (); 
    int year = Cal.get (calendar.year); 
    int month = Cal.get (calendar.month) + 1; 
    int day = Cal.get (calendar.day_of_month); 
    int hour = Cal.get (Calendar.hour_of_day); 
    int minute = Cal.get (Calendar.minute); 
    int second = Cal.get (Calendar.second); 
    System.out.println ("= = = = = = =" + year); 
    System.out.println ("= = = Month = = =" + month); 
    System.out.println ("= = = Day = = =" + days); 
    System.out.println ("= = = = When = =" + hour); 
    System.out.println ("= = = = = = = = =" + minute); System.out.println("= = = Second = = =" + second); } 
}

I hope this article will help you with your Java programming.

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.