Example of java date format am pm.

Example of java date format am pm.


Posted in : Core Java Posted on : November 23, 2010 at 5:47 PM Comments : [ 0 ]

Example of java date format am pm.

Example of java date format am pm.

In this section, you will learn how to convert time into 12-Hour format  or in AM PM format.

Here, Date() is a constructor of Date class. It creates a object of date and initialize it. The formatAMPM is object of String class. It stores the values of time format, in which we wants to change. And after it, we passes it into SimpleDateFormat class constructor. The SimpleDateFormat class use for formatting and parsing dates and format() method of this class is used for formatting Date class object into date / time into particular format and appends it into the StringBuffer.  

Date pattern  1 :- "hh:mm:ss a"  -( hour : minutes : second AP/PM)
                        2 :- "dd MMM yyy hh:mm:ss a"  - (date month year  hour : minutes : second AP/PM)

Code:
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateFormatAMPM {
  public static void main(String[] args) {
    // Create object of Date class an initialize it
    Date obDate = new Date();
    // Define format of time in to AM PM
    String formatAMPM = "hh:mm:ss a";
    // create object of SimpleDateFormat class
    // And pass format of time in it as argument
    SimpleDateFormat obDateFormat = new SimpleDateFormat(formatAMPM);
	System.out.println("Current Time of system in 12-hour format : "
	+ obDateFormat.format(obDate));
    String formatAMPM1 = "dd MMM yyyy hh:mm:ss a";
	SimpleDateFormat obDateFormat1 = new SimpleDateFormat(formatAMPM1);
    System.out
    .println("Current Time of system in 12-hour format with date : "
	+ obDateFormat1.format(obDate));
  }
}
Output
Current Time of system in 12-hour format : 05:35:02 PM
Current Time of system in 12-hour format wit date : 22 Nov 2010 05:35:02 PM

Download this code.

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics