-
Notifications
You must be signed in to change notification settings - Fork 0
/
TimeFormat.java
41 lines (34 loc) · 960 Bytes
/
TimeFormat.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.javamultiplex.datetime;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
*
* @author Rohit Agarwal
* @category Date and Time
* @problem How to display time in different formats?
*
*/
public class TimeFormat {
public static void main(String[] args) {
//Creating Date class instance.
Date date=new Date();
DateFormat timeFormat=null;
String time=null;
/*
* h -> Hour in day(1-12)
* H -> Hour in day(0-23)
* m -> Minute in hour
* s -> Second in minute
* a -> AM/PM
*/
timeFormat=new SimpleDateFormat("hh:mm:ss a");
//Convert Date to String.
time=timeFormat.format(date);
System.out.println("Current time in 12 hour format : "+time);
timeFormat=new SimpleDateFormat("HH:mm:ss a");
//Convert Date to String.
time=timeFormat.format(date);
System.out.println("Current time in 24 hour format : "+time);
}
}