Convert date and time to UTC

I have a date string 2012-11-21 13:11:25 that I get from the local database. I need to convert it according to UTC settings and display it on a specific screen. Therefore, if its GMT+05:30 it should be displayed as 2012-11-21 18:41:25 on the screen. How can I do this conversion. I checked some questions, but this did not work.

I can get a Date object that returns something like Wed Nov 21 13:11:25 GMT+05:30 2012 , after that I need to get the time as 18:41:25 and date as 11-21-2012

Thanks in advance

+10
android date time utc
source share
4 answers

Your df and inputFmt should use the same format.

But I think you should do it like this:

  Date myDate = new Date(); Calendar calendar = Calendar.getInstance(); calendar.setTimeZone(TimeZone.getTimeZone("UTC")); calendar.setTime(myDate); Date time = calendar.getTime(); SimpleDateFormat outputFmt = new SimpleDateFormat("MMM dd, yyy h:mm a zz"); String dateAsString = outputFmt.format(time); System.out.println(dateAsString); 
+16
source share

Get UTC from current time:

 public String getCurrentUTC(){ Date time = Calendar.getInstance().getTime(); SimpleDateFormat outputFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); outputFmt.setTimeZone(TimeZone.getTimeZone("UTC")); return outputFmt.format(time); } 
+11
source share

The best way to get a formatted Date string in the required format is

 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US); dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); String formatted = dateFormat.format(date); 
+3
source share
  //This is my input date String dtStart = "2019-04-24 01:22 PM"; SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm a"); Date date = null; try { date = format.parse(dtStart); getDateInUTC(date) } catch (ParseException e) { e.printStackTrace(); } 

// This method is used to convert the date to some UTC format

 public static String getDateInUTC(Date date) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); sdf.setTimeZone(TimeZone.getTimeZone("UTC")); String dateAsString = sdf.format(date); System.out.println("UTC" + dateAsString); return dateAsString; } 
0
source share

All Articles