Java Data uses the DataFormat class to simply format __java

Source: Internet
Author: User
Tags date now dateformat locale
A simple format for Java data

The need to obtain the current system time is often used in the programming process, and may be used to detect system performance or for other considerations.
Normally we need to format the current system time, and the process is rather annoying, but Java has a class that helps us solve the problem simply.

Java.text.DateFormat class is very convenient for us to deal with the format of the time

DateFormat is an abstract class of date/time format subclasses that format and parse dates or times in a language-independent way. Date/time formatting subclasses (such as SimpleDateFormat) allow formatting (that is, date-> text), parsing (text-> date), and normalization. Represents a date as a day object, or as a number of milliseconds from GMT (Greenwich Mean Time), January 1, 1970 00:00:00 This moment.

DateFormat provides a number of class methods for obtaining a default date/time Formatter based on the default or given locale and a variety of formatting styles. Formatting styles include full, LONG, MEDIUM, and short. More details and examples of how to use these styles are provided in the method description.

DateFormat can help you format and resolve dates for any locale. For months, weeks, and even calendar formats (lunar and Gregorian), their code is entirely independent of the language environment's conventions.

To format a date in the current locale, you can use a static factory method:

String myString = dateformat.getdateinstance (). Format (mydate);

To format a date for a different locale, you can specify it in the call to Getdateinstance ().

DateFormat df = dateformat.getdateinstance (Dateformat.long, locale.france);

You can also use DateFormat for parsing.

MyDate = Df.parse (myString);

Use Getdateinstance to get the standard date format for the country/region. Additional static factory methods are also available. Use Gettimeinstance to get the time format for the country/region. Use Getdatetimeinstance to get date and time formats. You can pass different options to these factory methods to control the length of the result (from short to MEDIUM to LONG to full). The exact results depend on the locale, but usually:

Short is entirely numeric, such as 12.13.52 or 3:30pm.
MEDIUM longer, such as the 12, 1952
Long longer, such as January 12, 1952 or 3:30:32pm
Full is fully specified, such as Tuesday, April 12, 1952 AD, or 3:30:42pm PST.
You can also set the time zone on the format, if you prefer. If you want to exert more control over formatting or parsing (or give users more control), you can try to cast the DateFormat obtained from the factory method to SimpleDateFormat. This applies to most countries/regions; Just remember to put it in a try block of code in case you encounter a special format.

You can also use the parseposition and fieldposition parsing and formatting methods to

Gradually parse the parts of the string.
Align any particular field, or find the location where the string is selected on the screen. The code is as follows

Import Java.text.DateFormat;
Import java.util.Date;


public class Time {public

    static void Main (String args[]) {

        date now = new Date ();
        DateFormat DTF = Dateformat.gettimeinstance ();
        DateFormat DDF = Dateformat.getdateinstance ();
        DateFormat DDTF = Dateformat.getdatetimeinstance ();

        System.out.println (now);
        System.out.println (Dtf.format (now));    
        System.out.println (Ddf.format (now));
        System.out.println (Ddtf.format (now));

    }

The above code can get four different results, has been a simple format, can be used for normal reading.

Fri APR 17:11:39 CST 2015
17:11:39
2015-4-3
2015-4-3 17:13:25

If more complex formatting is required, DateFormat provides some special formatting methods that pass in the constant code of type int when using Getxxxinstance

Import Java.text.DateFormat;
Import java.util.Date;


public class Time {public

    static void Main (String args[]) {

        date now = new Date ();
        DateFormat DTF = dateformat.gettimeinstance (1);
        DateFormat DDF = dateformat.getdateinstance (1);

        System.out.println (Dtf.format (now));    
        System.out.println (Ddf.format (now));

    }

You can see the results as follows
05:15 P.M. 01 seconds.
April 3, 2015

The specific type constants passed in can be passed into different parameters according to the individual requirements
For example:
Dateformat.long
Dateformat.medium
Dateformat.default
Dateformat.short
Dateformat.full

We can test it out for ourselves.

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.