SimpleDateFormat Hour Time format
In this chapter you will learn:
- Add AM PM to time format using SimpleDateFormat
- Formatting hour using SimpleDateFormat
- Formatting hour using SimpleDateFormat: k
Add AM PM to time format using SimpleDateFormat
import java.text.SimpleDateFormat;
import java.util.Date;
/*j a v a2 s.co m*/
public class Main {
public static void main(String[] args) {
Date date = new Date();
// formatting time to have AM/PM text using 'a' format
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss a");
System.out.println("Time with AM/PM field: " + sdf.format(date));
}
}
The output:
Formatting hour using SimpleDateFormat
import java.text.SimpleDateFormat;
import java.util.Date;
/*from ja va 2 s. c om*/
public class Main {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("h");
System.out.println("hour in h format : " + sdf.format(date));
sdf = new SimpleDateFormat("hh");
System.out.println("hour in hh format : " + sdf.format(date));
sdf = new SimpleDateFormat("H");
System.out.println("hour in H format : " + sdf.format(date));
sdf = new SimpleDateFormat("HH");
System.out.println("hour in HH format : " + sdf.format(date));
}
}
The output:
Formatting hour using SimpleDateFormat: k
import java.text.SimpleDateFormat;
import java.util.Date;
// j a va2 s . c o m
public class Main {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("k");
System.out.println("hour in k format : " + sdf.format(date));
sdf = new SimpleDateFormat("kk");
System.out.println("hour in kk format : " + sdf.format(date));
sdf = new SimpleDateFormat("K");
System.out.println("hour in K format : " + sdf.format(date));
sdf = new SimpleDateFormat("KK");
System.out.println("hour in KK format : " + sdf.format(date));
}
}
The output:
Next chapter...
What you will learn in the next chapter:
Home » Java Tutorial » Date, Time