Skip to main content

How to Use DateFormat Class in Java?

--
DateFormat helps you to format and parse dates for any locale. DateFormat provides many class methods for obtaining default date/time formatters based on the default or a given locale and a number of formatting styles. The formatting styles include FULL, LONG, MEDIUM, and SHORT. You can pass in different formatting style to these factory methods to control the length of the result but the exact result also depends on the locale, but generally:
  • SHORT is completely numeric, such as 12.13.52 or 3:30pm
  • MEDIUM is longer, such as Jan 12, 1952
  • LONG is longer, such as January 12, 1952 or 3:30:32pm
  • FULL is pretty completely specified, such as Tuesday, April 12, 1952 AD or 3:30:42pm PST.
Use getDateInstance to get the normal date format for that country. For example,
import java.text.DateFormat;
import java.util.Date;

public class DateFormatExample {

    public static void main(String[] args) {
        
        Date now = new Date();

        DateFormat defaultDf = DateFormat.getDateInstance();
        DateFormat shortDf = DateFormat.getDateInstance(DateFormat.SHORT);
        DateFormat mediumDf = DateFormat.getDateInstance(DateFormat.MEDIUM);
        DateFormat longDf = DateFormat.getDateInstance(DateFormat.LONG);
        DateFormat fullDf = DateFormat.getDateInstance(DateFormat.FULL);
        
        System.out.println(" 1. " + defaultDf.format(now));
        System.out.println(" 2. " + shortDf.format(now));
        System.out.println(" 3. " + mediumDf.format(now));
        System.out.println(" 4. " + longDf.format(now));
        System.out.println(" 5. " + fullDf.format(now));
    }
}
The output is
1. Jun 20, 2008
 2. 6/20/08
 3. Jun 20, 2008
 4. June 20, 2008
 5. Friday, June 20, 2008
 
Use getTimeInstance to get the time format for that country. For example,
import java.text.DateFormat;
import java.util.Date;

public class DateFormatExample {

    public static void main(String[] args) {
        
        Date now = new Date();

        DateFormat defaultDf = DateFormat.getTimeInstance();
        DateFormat shortDf = DateFormat.getTimeInstance(DateFormat.SHORT);
        DateFormat mediumDf = DateFormat.getTimeInstance(DateFormat.MEDIUM);
        DateFormat longDf = DateFormat.getTimeInstance(DateFormat.LONG);
        DateFormat fullDf = DateFormat.getTimeInstance(DateFormat.FULL);
        
        System.out.println(" 1. " + defaultDf.format(now));
        System.out.println(" 2. " + shortDf.format(now));
        System.out.println(" 3. " + mediumDf.format(now));
        System.out.println(" 4. " + longDf.format(now));
        System.out.println(" 5. " + fullDf.format(now));
    }
}
The output is
1. 10:09:12 PM
 2. 10:09 PM
 3. 10:09:12 PM
 4. 10:09:12 PM EDT
 5. 10:09:12 PM EDT
 
Use getDateTimeInstance to get a date and time format. For example,
import java.text.DateFormat;
import java.util.Date;

public class DateFormatExample {

    public static void main(String[] args) {
  
        Date now = new Date();

        DateFormat defaultDf = DateFormat.getDateTimeInstance();
        DateFormat shortDf = DateFormat.getDateTimeInstance(
               DateFormat.SHORT, DateFormat.SHORT);
        DateFormat mediumDf = DateFormat.getDateTimeInstance(
               DateFormat.MEDIUM, DateFormat.SHORT);
        DateFormat longDf = DateFormat.getDateTimeInstance(
               DateFormat.LONG, DateFormat.SHORT);
        DateFormat fullDf = DateFormat.getDateTimeInstance(
               DateFormat.FULL, DateFormat.SHORT);
        
        System.out.println("  1. " + defaultDf.format(now));
        System.out.println("  2. " + shortDf.format(now));
        System.out.println("  3. " + mediumDf.format(now));
        System.out.println("  4. " + longDf.format(now));
        System.out.println("  5. " + fullDf.format(now));
        System.out.println("============================");
        shortDf = DateFormat.getDateTimeInstance(DateFormat.SHORT, 
              DateFormat.MEDIUM);
        mediumDf = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, 
              DateFormat.MEDIUM);
        longDf = DateFormat.getDateTimeInstance(DateFormat.LONG, 
              DateFormat.MEDIUM);
        fullDf = DateFormat.getDateTimeInstance(DateFormat.FULL, 
              DateFormat.MEDIUM);
     
        System.out.println("  6. " + shortDf.format(now));
        System.out.println("  7. " + mediumDf.format(now));
        System.out.println("  8. " + longDf.format(now));
        System.out.println("  9. " + fullDf.format(now));
        System.out.println("============================");
        
        shortDf = DateFormat.getDateTimeInstance(DateFormat.SHORT, 
              DateFormat.LONG);
        mediumDf = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, 
              DateFormat.LONG);
        longDf = DateFormat.getDateTimeInstance(DateFormat.LONG, 
              DateFormat.LONG);
        fullDf = DateFormat.getDateTimeInstance(DateFormat.FULL, 
             DateFormat.LONG);
     
        System.out.println(" 10. " + shortDf.format(now));
        System.out.println(" 11. " + mediumDf.format(now));
        System.out.println(" 12. " + longDf.format(now));
        System.out.println(" 13. " + fullDf.format(now));
        System.out.println("============================");

        shortDf = DateFormat.getDateTimeInstance(DateFormat.SHORT, 
                DateFormat.FULL);
        mediumDf = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, 
                DateFormat.FULL);
        longDf = DateFormat.getDateTimeInstance(DateFormat.LONG, 
                DateFormat.FULL);
        fullDf = DateFormat.getDateTimeInstance(DateFormat.FULL, 
                DateFormat.FULL);
     
        System.out.println(" 14. " + shortDf.format(now));
        System.out.println(" 15. " + mediumDf.format(now));
        System.out.println(" 16. " + longDf.format(now));
        System.out.println(" 17. " + fullDf.format(now));

    }

}
The output is
1. Jun 21, 2008 9:30:41 PM
  2. 6/21/08 9:30 PM
  3. Jun 21, 2008 9:30 PM
  4. June 21, 2008 9:30 PM
  5. Saturday, June 21, 2008 9:30 PM
============================
  6. 6/21/08 9:30:41 PM
  7. Jun 21, 2008 9:30:41 PM
  8. June 21, 2008 9:30:41 PM
  9. Saturday, June 21, 2008 9:30:41 PM
============================
 10. 6/21/08 9:30:41 PM EDT
 11. Jun 21, 2008 9:30:41 PM EDT
 12. June 21, 2008 9:30:41 PM EDT
 13. Saturday, June 21, 2008 9:30:41 PM EDT
============================
 14. 6/21/08 9:30:41 PM EDT
 15. Jun 21, 2008 9:30:41 PM EDT
 16. June 21, 2008 9:30:41 PM EDT
 17. Saturday, June 21, 2008 9:30:41 PM EDT

Comments

Popular posts from this blog

WebSphere MQ Interview Questions

What is MQ and what does it do? Ans. MQ stands for MESSAGE QUEUEING. WebSphere MQ allows application programs to use message queuing to participate in message-driven processing. Application programs can communicate across different platforms by using the appropriate message queuing software products. What is Message driven process? Ans . When messages arrive on a queue, they can automatically start an application using triggering. If necessary, the applications can be stopped when the message (or messages) have been processed. What are advantages of the MQ? Ans. 1. Integration. 2. Asynchrony 3. Assured Delivery 4. Scalability. How does it support the Integration? Ans. Because the MQ is independent of the Operating System you use i.e. it may be Windows, Solaris,AIX.It is independent of the protocol (i.e. TCP/IP, LU6.2, SNA, NetBIOS, UDP).It is not required that both the sender and receiver should be running on the same platform What is Asynchrony? Ans. With messag...

Asynchronous Vs. Synchronous Communications

Synchronous (One thread):   1 thread -> |<---A---->||<----B---------->||<------C----->| Synchronous (multi-threaded):   thread A -> |<---A---->| \ thread B ------------> ->|<----B---------->| \ thread C ----------------------------------> ->|<------C----->|

Solution for the Error : An internal error occured during: "reload maven project"

You might have faced this issue after changing the project name or may be changing project name in pom.xml. When you restart your eclipse it may not launch due to project metadata file is corrupted due to your .project name changes and Maven can not laod your project properly and will displays this error shown above and eclipse console will not be opened. To get rid off this issue follow the solution given below