Shakti Sharma
Shakti Sharma

Reputation: 2309

convert date and time into selected timezone

i have a list of time zones of diffrent countries in a selectbox and have a textbox where user can enter date and time in a specified format only which is yyyy-MM-dd hh:mm:ss.

i want entered timezone to be converted in to selcted timezone from list. How can i do this.

<option value="Pacific/Apia">( GMT -11:0 ) West Samoa Time ( Pacific/Apia )</option>
<option value="Pacific/Midway">( GMT -11:0 ) Samoa Standard Time ( Pacific/Midway )</option>
<option value="Pacific/Niue">( GMT -11:0 ) Niue Time ( Pacific/Niue )</option>
<option value="Pacific/Pago_Pago">( GMT -11:0 ) Samoa Standard Time ( Pacific/Pago_Pago )</option>
<option value="Pacific/Samoa">( GMT -11:0 ) Samoa Standard Time ( Pacific/Samoa )</option>
<option value="US/Samoa">( GMT -11:0 ) Samoa Standard Time ( US/Samoa )</option>

Textbox

<input type="text" name="createdOnGMTDate" id="createdOnGMTDate"  style="width:200px;"/>

Thanks

Upvotes: 0

Views: 88

Answers (2)

JavaEE guy
JavaEE guy

Reputation: 174

You can use SimpleDateFormat to format date with a certain pattern with a predefined locale. For example:

Calendar cal = Calendar.getInstance(locale);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("GMT-11"));
String now = sdf.format(cal.getTime());

Upvotes: 1

Nigam Patro
Nigam Patro

Reputation: 2770

Try below code, may be this will help you.

DateFormat formatter= new SimpleDateFormat("MM/dd/yyyy HH:mm:ss Z");
formatter.setTimeZone(TimeZone.getTimeZone("Europe/London"));
System.out.println(formatter.format(date));

formatter.setTimeZone(TimeZone.getTimeZone("Europe/Athens"));
System.out.println(formatter.format(instance2.getTime()))

Upvotes: 0

Related Questions