Get Time in MEDIUM format

V

Vera

This code outputs the date and the time that an event occurs, but how
do I tell it to only output the time in MEDIUM format? I know this is
easy but I just don't see how to do it :(

/** Method to Get Time of an Event */
public static String getTimeOfEvent()
{
GregorianCalendar calendar = new GregorianCalendar();
DateFormat formatter =
DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
DateFormat.MEDIUM, Locale.US);
TimeZone timeZone = TimeZone.getTimeZone("CST");
formatter.setTimeZone(timeZone);
String outputTime = formatter.format(calendar.getTime());
return outputTime;
}

Thank you :)
 
I

Infra Dig

Vera said:
This code outputs the date and the time that an event occurs, but how
do I tell it to only output the time in MEDIUM format? I know this is
easy but I just don't see how to do it :(

/** Method to Get Time of an Event */
public static String getTimeOfEvent()
{
GregorianCalendar calendar = new GregorianCalendar();
DateFormat formatter =
DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
DateFormat.MEDIUM, Locale.US);
TimeZone timeZone = TimeZone.getTimeZone("CST");
formatter.setTimeZone(timeZone);
String outputTime = formatter.format(calendar.getTime());
return outputTime;
}

Thank you :)

Your code produces...

Oct 9, 2006 9:50:28 PM

....on my system. That looks correct to me -- i.e., both date and time are
in MEDIUM format.

If I understand your question, you want to output the time only, like so...

9:50:28 PM


If that's what you want to do, then change this...

DateFormat formatter =
DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
DateFormat.MEDIUM, Locale.US);


....to this...

DateFormat formatter =
DateFormat.getTimeInstance(DateFormat.MEDIUM, Locale.US);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,225
Members
46,815
Latest member
treekmostly22

Latest Threads

Top