SimpleDateFormat to format Date object using custom format string - Java Date Time

Java examples for Date Time:SimpleDateFormat

Description

SimpleDateFormat to format Date object using custom format string

Demo Code



import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class LocalizationExamples {

    public static void main(String[] args) {

        /**//  www  .  ja  v  a  2  s  .  co  m
         * Run this, and try to guess the pattern that generated every output!
         */
        SimpleDateFormat simpleDateFormat = null;
        String dateAsString = null;

        System.out.println( "Date 1:\n" );
        simpleDateFormat = new SimpleDateFormat("dd/MM/yy");
        dateAsString = simpleDateFormat.format(new Date());
        System.out.println(dateAsString);

        System.out.println( "Date 2:\n" );
        simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd G 'at' HH:mm:ss z");
        dateAsString = simpleDateFormat.format(new Date());
        System.out.println(dateAsString);

        System.out.println( "Date 3:\n" );
        simpleDateFormat = new SimpleDateFormat("EEE, MMM d, ''yy");
        dateAsString = simpleDateFormat.format(new Date());
        System.out.println(dateAsString);

        System.out.println( "Date 4:\n" );
        simpleDateFormat = new SimpleDateFormat("h:mm a");
        dateAsString = simpleDateFormat.format(new Date());
        System.out.println(dateAsString);

        System.out.println( "Date 5:\n" );
        simpleDateFormat = new SimpleDateFormat("hh 'o''clock' a, zzzz");
        dateAsString = simpleDateFormat.format(new Date());
        System.out.println(dateAsString);

        System.out.println( "Date 6:\n" );
        simpleDateFormat = new SimpleDateFormat("K:mm a, z");
        dateAsString = simpleDateFormat.format(new Date());
        System.out.println(dateAsString);

        System.out.println( "Date 7:\n" );
        simpleDateFormat = new SimpleDateFormat("yyyyy.MMMMM.dd GGG hh:mm aaa");
        dateAsString = simpleDateFormat.format(new Date());
        System.out.println(dateAsString);

        System.out.println( "Date 8:\n" );
        simpleDateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
        dateAsString = simpleDateFormat.format(new Date());
        System.out.println(dateAsString);

        System.out.println( "Date 9:\n" );
        simpleDateFormat = new SimpleDateFormat("yyMMddHHmmssZ");
        dateAsString = simpleDateFormat.format(new Date());
        System.out.println(dateAsString);

        System.out.println( "Date 10:\n" );
        simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
        dateAsString = simpleDateFormat.format(new Date());
        System.out.println(dateAsString);

        System.out.println( "Date 11:\n" );
        simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
        dateAsString = simpleDateFormat.format(new Date());
        System.out.println(dateAsString);

        System.out.println( "Date 12:\n" );
        simpleDateFormat = new SimpleDateFormat("YYYY-'W'ww-u");
        dateAsString = simpleDateFormat.format(new Date());
        System.out.println(dateAsString);

        System.out.println( "Date 13:\n" );
        simpleDateFormat = new SimpleDateFormat("hh 'o''clock' a, zzzz", Locale.FRANCE);
        dateAsString = simpleDateFormat.format(new Date());
        System.out.println(dateAsString);
    }
}

Related Tutorials