Tags

,

— Lets you format the date in user defined patterns.

— The formats are specified by date and time pattern strings where letters represents date or time components.

— It came in handy when I had to enter date in specific formats while automating.

Few examples:


package Dateformatting;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Dateformatting {

public static void main(String[] args) {

// creating date object
Date today = new Date();

SimpleDateFormat formatter1 = new SimpleDateFormat("yyyyMMdd");
String date1 = formatter1.format(today);
System.out.println("Today's date in format yyyyMMdd: " + date1);
// Output = Today's date in format yyyyMMdd: 20150220

SimpleDateFormat formatter2 = new SimpleDateFormat("dd/MM/yyyy");
String date2 = formatter2.format(today);
System.out.println("Today's date in format dd/MM/yyyy: " + date2);
// Output = Today's date in format dd/MM/yyyy: 20/02/2015

SimpleDateFormat formatter3 = new SimpleDateFormat("dd-MMM-yy");
String date3 = formatter3.format(today);
System.out.println("Today's date in format dd-MMM-yy: " + date3);
// Output = Today's date in format dd-MMM-yy: 20-Feb-15

SimpleDateFormat formatter4 = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss a z");
// z represents the time zone, a refers to AM/PM
String date4 = formatter4.format(today);
System.out.println("Today's date in format YYYY-MM-dd HH:mm:ss z: " + date4);
// Output = Today's date in format YYYY-MM-dd HH:mm:ss z: 2015-02-20 11:09:00 AM EST

SimpleDateFormat formatter5 = new SimpleDateFormat("hh 'o''clock' a zzzz");
// z represents the time zone, a refers to AM/PM
String date5 = formatter5.format(today);
System.out.println("Today's date in format hh 'o''clock' a zzzz: " + date5);
// Output = Today's date in format hh 'o''clock' a zzzz: 11 o'clock AM Eastern Summer Time (New South Wales)

SimpleDateFormat formatter6 = new SimpleDateFormat("MMM dd, yyyy E X");
// E represents day of week , X refers to timezone
String date6 = formatter6.format(today);
System.out.println("MMM dd, yyyy F X: " + date6);
// Output = Today's date in format hh 'o''clock' a zzzz: 11 o'clock AM Eastern Summer Time (New South Wales)

// Using toString function
System.out.println("Using ToString: " + today.toString());
// Output : Using ToString: Fri Feb 20 12:04:06 EST 2015

// Using DateFormat and getDateInstance method
System.out.println("Using DateFormat: " + DateFormat.getDateInstance().format(today));
// Output : Using DateFormat: Feb 20, 2015

// Using DateFormat and getInstance method
System.out.println("Using DateFormat: " + DateFormat.getInstance().format(today));
// Output : Using DateFormat: 2/20/15 12:11 PM

// Using DateFormat and getDateTimeInstance method
System.out.println("Using DateFormat: " + DateFormat.getDateTimeInstance().format(today));
// Output: Using DateFormat: Feb 20, 2015 12:11:05 PM

// Using DateFormat and getTimeInstance method
System.out.println("Using DateFormat: " + DateFormat.getTimeInstance().format(today));
// Output: Using DateFormat: 12:12:09 PM

}

}

All the above examples involve passing a date object, which is what I needed to do for now.


					
Advertisements