Java uses the string class to format the current date implementation code _java

Source: Internet
Author: User
Tags locale

When outputting date information, it is often necessary to output date formats in different formats, and this example describes the date formatting methods in the string string class, which use different ways to output the date format parameter values of the string class, which can be combined to implement a date string in a special format.

Ideas are as follows: For example, to output the English abbreviation of the month, the format () method of the String class, the first parameter specifies that the locale is locale.us, the default is a number, the second parameter is%TB for the month abbreviation, and the third parameter is the date () class object.

The code is as follows:

Copy Code code as follows:

Import Java.util.Date;
Import Java.util.Locale;

public class Example1 {
public static void Main (string[] args) {
Date today = new Date ();
The formatted string is the English abbreviation for the month
String a = String.Format (locale.us, "%TB", today);
System.out.println ("formatted string is the English abbreviation for the Month:" + a);
The formatted string is written in English all for the month
String B = String.Format (locale.us, "%TB", today);
System.out.println ("formatted string is the English abbreviation for the month:" + b);
The formatted string is a week (e.g., Monday)
String C = String.Format ("%ta", today);
SYSTEM.OUT.PRINTLN ("Month formatted string is week:" + C);
The formatted string is a week (e.g., Monday)
String d = String.Format ("%ta", today);
System.out.println ("Formatted string is week:" + D);
The formatted string is a 4-bit year value
String e = String.Format ("%ty", today);
SYSTEM.OUT.PRINTLN ("The formatted string is a 4-bit year value:" + E);
The formatted string is a 2-bit year value
String f = String.Format ("%ty", today);
SYSTEM.OUT.PRINTLN ("The formatted string is a 2-bit year value:" + f);
The formatted string is a 2-bit month value
String g = String.Format ("%tm", today);
System.out.println ("formatted string is a 2-bit month value:" + g);
The formatted string is a 2-bit date value
String h = String.Format ("%td", today);
System.out.println ("formatted string is a 2-bit date value:" + h);
The formatted string is a 1-bit date value
String i = String.Format ("%te", today);
System.out.println ("formatted string is a 1-bit date value:" + i);
}
}

Effect as shown:

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.