Java – How to convert Date into UTC in Java

datedate-conversionjava

Say apology to ask repeated Question but i'm so confused to convert this time into UTC.

In my Application taking current System Date

Date date1 = new Date();//Fri Jul 26 10:34:06 IST 2013
SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss ");

then i have a drop down list (i'm using Selectbox timezone list)

if an indian user select GMT+05:30

now i want to convert date1 in UTC time using GMT+05:30 like Fri Jul 26 05:00:06 IST 2013

So Every user select some timezone only but in that timezone using server current Date and Time it will be convert to UTC time

Please help me.

Best Answer

Try this

SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.HOUR_OF_DAY, 5);
cal.add(Calendar.MINUTE, 30);
System.out.println(f.format(cal.getTime()));