O'Reilly logo

Java Garage by Eben Hewitt

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Time

The following class contains methods for formatting times in ways you are likely to want to do.

Times.java

 package net.dates; import java.util.*; import java.text.*; public class Times { Calendar calendar = Calendar.getInstance(); public static void main(String...a) { log(get24HourTime() ); log(getAMPMTime() ); log( getSimpleTime() ); } /** * Returns time in this format: * 14.36.33 */ public static String get24HourTime() { Date d = new Date(); Format formatter = new SimpleDateFormat("HH.mm.ss"); return formatter.format(d); } /** * Returns time in this format: * 01:12:53 AM */ public static String getAMPMTime() { Format formatter = new SimpleDateFormat("hh:mm:ss a"); return formatter.format(new Date()); } public static String getSimpleTime() ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required