Java Data Type How to - date 12am is stored as 24, kk:mm:ss instead of HH:mm:ss








Question

We would like to know how to date 12am is stored as 24, kk:mm:ss instead of HH:mm:ss.

Answer

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
//from ww w . j  ava 2  s  .com
public class Main {
  public static void main(String[] args) throws Exception {
    SimpleDateFormat broken = new SimpleDateFormat("kk:mm:ss");
    broken.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
    SimpleDateFormat working = new SimpleDateFormat("HH:mm:ss");
    working.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));

    Date epoch = new Date(0);

    System.out.println(broken.format(epoch));
    System.out.println(working.format(epoch));
  }
}

The code above generates the following result.