32376

converting UTC date string to local date string inspecific format

Question:

I have a UTC date in string

String utcDate = "2014-03-05 07:09:07.0";

I want to convert it to local date string of format DD-MMM-YYYY hh:mm a eg: 5-Mar-2014 12:39 PM from UTC date 2014-03-05 07:09:07.0

How this can be achieved using simple java or joda API

Answer1:

Very easy to achieve with default functionality. I hope the local data is for display only.

SimpleDateFormat parser = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S"); parser.setTimeZone(TimeZone.getTimeZone("UTC")); Date parsed = parser.parse(utcDate); SimpleDateFormat formatter = new SimpleDateFormat("d-MMM-yyyy hh:mm a"); System.out.println(formatter.format(parsed));

Answer2:

The java.util.Date and .Calendar classes are notoriously troublesome. Avoid them. Instead use either <a href="http://www.joda.org/joda-time/" rel="nofollow">Joda-Time</a> library or the new <a href="http://download.java.net/jdk8/docs/api/java/time/package-summary.html" rel="nofollow">java.time package</a> in bundled with <a href="https://en.wikipedia.org/wiki/Java_version_history#Java_SE_8" rel="nofollow">Java 8</a>.

If you use the <a href="https://en.wikipedia.org/wiki/ISO_8601" rel="nofollow">ISO 8601</a> format of strings, you can pass the string directly to a Joda-Time <a href="http://www.joda.org/joda-time/apidocs/org/joda/time/DateTime.html" rel="nofollow">DateTime</a> constructor. Your input string is close, but the space in the middle should be a T.

Some example code using the Joda-Time 2.3 library.

String input = "2014-03-05 07:09:07.0"; String inputModified = input.replace( " ", "T" ); DateTimeZone timeZone = DateTimeZone.forID( "Europe/Paris" ); DateTime dateTimeUtc = new DateTime( inputModified, DateTimeZone.UTC ); DateTime dateTimeParis = dateTimeUTC.toZone( timeZone ); String outputFrance = DateTimeFormat.forPattern( "FF" ).withLocale(Locale.FRANCE).print( dateTimeParis ); DateTimeFormatter formatter = DateTimeFormat.forPattern( "d-MMM-yyyy hh:mm a" ).withLocale( Locale.US ); String outputParisCustom = formatter.print( dateTimeParis );

Answer3:

Below code will help you to convert your UTC to IST or any other timezone. You need to pay attention to the timezone that you want to use with SimpleDateFormat.

import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.TimeZone; public class ConvertTimeZone { public static void main(String args[]) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); sdf.setTimeZone(TimeZone.getTimeZone("UTC")); Date date = sdf.parse("2014-03-05 07:09:07"); System.out.println("time in UTC " +sdf.format(date)); sdf.setTimeZone(TimeZone.getTimeZone("IST")); System.out.println("Time in IST is " + sdf.format(date)); } }

Recommend

  • Cakephp Fatal error: Call to a member function format() on a non-object
  • Google date on spreadsheet is different than date in script logger. Is this a time zone issue?
  • How to format date without year using Simple Date Format?
  • java: how to get Timezone abbreviation (from offset)?
  • highlighting specific date in calendar view without onClickListener
  • Jackon 2.4.2 failing to deserialize valid date, even after specifying date format
  • DateTime alters timestamp during DST winter change
  • NSDateComponents returning strange hour
  • Parsing Java String to date
  • Firebase Save Notification to DB not working when app is not running
  • how to find the index position of the ARRAY Where NSPredicate pick the value. I use filteredArrayUsi
  • Formatting a TimeSpan to look like a time zone offset
  • problem with calling libraries
  • html5 tag support
  • Parsing Java dates with truncated timezone information
  • java program to calculate XIRR without using excel or any other library
  • Visual Studio 2010 - 2015 does not use ymm* registers for AVX optimization
  • A simple datepicker in VueJS
  • How to make Plotly chart with year mapped to line color and months on x-axis
  • How to change curtime?
  • How to parsing NSDate to RFC 822 always use in English?
  • date: illegal option — d, Find difference between two dates
  • iOS Date formatting
  • Google Calendar Api is not showing event list
  • date format change with DT and shiny
  • Get localized short date pattern as String?
  • Find Previous month name using Calender or any classes that in java
  • Illegal mix of collations for operation for date/time comparison
  • Microsoft Visual Studio Community 2015 always crashes in Windows 10 if swithed to Visual FoxPro
  • ActionScript 2 vs ActionScript 3 performance
  • Release, debug version and Authorization Google?
  • To display the title for the current loaction in map in iphone
  • Traverse Array and Display in markup
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • How to set the response of a form post action to a iframe source?
  • Hits per day in Google Big Query
  • embed rChart in Markdown
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • How can I use `wmic` in a Windows PE script?
  • Unable to use reactive element in my shiny app