Sunday, April 5, 2020

java DateFormat class

DateFormat class

The java.text.DateFormat class is the subclass of Format class. This is an abstract class and provides date/time formatting subclasses. We can format and parse date and time in a language-independent manner.

The formatting and parsing are two different processes opposite to each other. Formatting is the process of converting from date to string, parsing is a conversion from string to date.

The subclasses of java.text.DateFormat class like SimpleDateFormat provides the methods for formatting, parsing, and normalization in any given locale. We can provide several formatting styles including FULL, LONG, MEDIUM, and SHORT also. The date can be represented as the object of java.util.Date class.

We can write code completely independent from the conventions for months, days of the week, or even the Calendar format. We can write code independent of locale or even the calendar type such as lunar or solar.

The getDateInstanceMethod() factory method

This is a factory method to get the date formatter with the given formatting style for the given locale.

We can format the date as,

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
public class DateDemo {
     //getInstance static factory method
       public static void main(String[] args) {
   String myString = DateFormat.getDateInstance().format(new Date());
   System.out.println(myString);
   String frenchString = DateFormat.getDateInstance(DateFormat.LONG, Locale.FRANCE).format(new Date());
        System.out.println(frenchString);
      }
}
Output:

5 Apr, 2020

5 avril 2020

The factory method getDateInstance() used to get the normal date format for any country. 
We can apply different options available for these factory methods to control the length of the output.
Length can be SHORT, MEDIUM, LONG, or FULL. The final results will vary according to the locale.


import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
public class DateDemo {

      public static void main(String[] args) {
            String engString = DateFormat.getDateInstance(DateFormat.SHORT, Locale.ENGLISH).format(new Date());
    System.out.println(engString);
engString = DateFormat.getDateInstance(DateFormat.LONG, Locale.ENGLISH).format(new Date());
        System.out.println(engString);
        engString = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.ENGLISH).format(new Date());
        System.out.println(engString);
        engString = DateFormat.getDateInstance(DateFormat.FULL, Locale.ENGLISH).format(new Date());
        System.out.println(engString);
      }
}
Output:
4/5/20
April 5, 2020
Apr 5, 2020
Sunday, April 5, 2020


We can also use getTimeInstance() and getDateTimeInstance() factory methods to to format the date object.

For example,

public class DateDemo {

     public static void main(String[] args) {
 String engString = DateFormat.getTimeInstance().format(new Date());
           System.out.println(engString);
 engString = DateFormat.getDateTimeInstance().format(new Date());
           System.out.println(engString);
          
     }
}
Output:
10:15:51 AM
5 Apr, 2020 10:15:51 AM



The getCalendar() method can be used to get the Calendar object from the DateFormat object.

import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateDemo {

 public static void main(String[] args) {
//Calendar Example
Calendar cal=DateFormat.getInstance().getCalendar();
System.out.println(cal.getTime());
     }
}
Output:
Fri Apr 05 10:24:50 IST 1940


The parsing the String


We can use the parse() method to parse the input string containing the date information. This method throws ParseException, if the input string is not parseable/valid then this exception is thrown,

import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
public class DateDemo {

      public static void main(String[] args) {
            String dateString="05 April, 2020, 10:24:50";
            //Parse a String with some date and time information
            try {
                  Date date = DateFormat.getDateInstance().parse(dateString);
                  System.out.println(date);
            } catch (ParseException e) {
                  System.out.println("Invalid input...");
                  e.printStackTrace();
            }
           
      }
}
Output:
Sun Apr 05 00:00:00 IST 2020



Reference: https://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html