English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I am writing a jsp where i select time thats in am pm format example 9 pm but when it sends it to the servelt using httprequest it is in the 24 hrs format ie 2100 ,now this is a String,how do i convert it to Date and then to am pm format

2006-11-08 14:58:23 · 2 answers · asked by alfia_09 1 in Computers & Internet Programming & Design

2 answers

int time = Integer.valueOf( stringtime ).intValue();

if (time >= 1300)
time=time-1200;

i think...

2006-11-08 15:05:32 · answer #1 · answered by VzjrZ 5 · 0 0

Use java.util.Calendar.

Calendar cal = Calendar.getInstance(); //gets "now"

Then use the set() method, the form of which depends on how customized you need to make your time. For your example, you need to break up 24-hour time into hours and minutes, e.g. 21 hours, 00 minutes, and then use the set() like this:

cal.set(Calendar.HOUR_OF_DAY, hour);
cal.set(Calendar.MINUTE, minutes);
cal.set(Calendar.SECOND, 0);
Date inputDate = cal.getTime();

Then just use either SimpleDateFormat or DateFormat to customize how you display it back out, e.g.
SimpleDateFormat formatter = new SimpleDateFormat("h:mm a");
String timeInAMorPM = formatter.format(inputDate);

I recommend using Calendar because it gives you more control (need it to represent yesterday or tomorrow's time?).

2006-11-09 03:59:35 · answer #2 · answered by vincentgl 5 · 0 0

fedest.com, questions and answers