Java date format problems

Source: Internet
Author: User
Tags dateformat

Code 1:

Get the current date using the date class

Public class mydate {public static void main (string [] ARGs) {date mydatestring = new date (); // instantiate the date object system. out. println ("current system date" + mydatestring); // output date }}

:

Code 2:

Obtain the current date using the calender class

Public class mydate02 {public static void main (string [] ARGs) {calendar = NULL; // declare a Calendar Object calendar = new gregoriancalendar (); // instantiate system by subclass. out. println ("year:" + calendar. get (calendar. year); // obtain the year system. out. println ("month:" + (calendar. get (calendar. month) + 1); // Add 1system to the month. out. println ("Date:" + calendar. get (calendar. day_of_month); // use day_of_monthsystem.out.println ("Hour" + calendar. get (calendar. hour_of_day); // use hour_of_daysystem.out.println ("Minutes" + calendar. get (calendar. minute); // minute system. out. println ("seconds" + calendar. get (calendar. second); // second system. out. println ("millisecond" + calendar. get (calendar. millisecond); // millisecond }}

:

Code 3:

Obtain the current date using the dateformat class

Public class mydate03 {public static void main (string [] ARGs) {dateformat DF1 = NULL; // declare dateformat object dateformat df2 = NULL; DF1 = dateformat. getdateinstance (); // get date df2 = dateformat. getdatetimeinstance (); // obtain the date and time system. out. println ("Date:" + df1.format (new date (); // format the date system. out. println ("datetime:" + df2.format (new date ()));}}

:

Code 4:

Set the date format based on code 3:

Public class mydate04 {public static void main (string [] ARGs) {dateformat DF1 = NULL; // declare dateformat object dateformat df2 = NULL; DF1 = dateformat. getdateinstance (dateformat. year_field, new locale ("ZH", "ch"); // get the date and set the date style df2 = dateformat. getdatetimeinstance (dateformat. year_field, dateformat. era_field, new locale ("ZH", "ch"); // obtain the date and time, and set the date display format and time display format system. out. println ("Date:" + df1.format (new date (); // format the date system. out. println ("datetime:" + df2.format (new date ()));}}

:

Not complete...

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.