59880

Format Date Java

How to format a string that looks like this

Sat Dec 08 00:00:00 JST 2012

into yyyy-mm-dd i.e.

2012-12-08

From browsing the web, I found this piece of code:

SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy"); String dateInString = "Sat Dec 08 00:00:00 JST 2012"; try { Date date = formatter.parse(dateInString); System.out.println(date); System.out.println(formatter.format(date)); } catch (ParseException e) { e.printStackTrace(); }

However, I am unable to modify it to accept the first line (Sat Dec 08 00:00:00 JST 2012) as a string and format that into the yyyy-mm-dd format.

What should I do about this? Should I be attempting to modify this? Or try another approach altogether?

Update: I'm using this from your answers <strong>(getting error: Unparseable date: "Sat Dec 08 00:00:00 JST 2012")</strong>

public static void main(String[] args) throws ParseException{ SimpleDateFormat srcFormatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.JAPANESE); SimpleDateFormat destFormatter = new SimpleDateFormat("dd-MMM-yyyy", Locale.JAPANESE); Date date = srcFormatter.parse("Sat Dec 08 00:00:00 JST 2012"); String destDateString = destFormatter.format(date); /* String dateInString = "Sat Dec 08 00:00:00 JST 2012";*/ System.out.println(destDateString); /*try { Date date = formatter.parse(dateInString); System.out.println(date); System.out.println(formatter.format(date)); } catch (ParseException e) { e.printStackTrace(); }*/ } }

Answer1:

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH); String dateInString = "Wed Oct 16 00:00:00 CEST 2013"; try { SimpleDateFormat parse = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.ENGLISH); Date date = parse.parse(dateInString); System.out.println(date); System.out.println(formatter.format(date)); } catch (ParseException e) { e.printStackTrace(); }

Change your formation to this new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);

Thanks..

Answer2:

Try this -

SimpleDateFormat srcFormatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.JAPANESE); SimpleDateFormat destFormatter = new SimpleDateFormat("dd-MMM-yyyy", Locale.JAPANESE); Date date = srcFormatter.parse("Sat Dec 08 00:00:00 JST 2012"); String destDateString = destFormatter.format(date);

Answer3:

You need two SimpleDateFormat objects. One to parse the date from the string using parse() method and the second one to output it in desired format using format() method. For more info about date formatting check the docs.

Recommend

  • Parsing date string (MM-dd) to java date in default year
  • How to make JDatePicker text field formatted for input?
  • java program to calculate XIRR without using excel or any other library
  • Access user's phone number on iOS 7
  • Reading a file into a multidimensional array
  • I18n locale disregarding fallbacks
  • close() was never explicitly called on database
  • App restarts from wrong activity
  • Should I or shouldn't I use the CachingConnectionFactory with hornetq 2.4.1
  • Groovy: Unexpected token “:”
  • Do I need to reset a Perl hash index?
  • Converting a WriteableBitmap image ToArray in UWP
  • Magento Fatal error: Maximum execution error solution, on WAMP
  • Word Open XML Mail Merge
  • Why querying a date BC is changed to AD in Java?
  • Row Count Is Returning the incorrect number using RaptureXML
  • Bad request using file_get_contents for PUT request in PHP
  • Jetty Server not starting: Unable to establish loopback connection
  • Reading JSON from a file using C++ REST SDK (Casablanca)
  • Python CGI os.system causing malformed header
  • Why value captured by reference in lambda is broken? [duplicate]
  • Spring Data JPA custom method causing PropertyReferenceException
  • FB SDK and cURL: Unknown SSL protocol error in connection to graph.facebook.com:443
  • Using $this when not in object context
  • Illegal mix of collations for operation for date/time comparison
  • How do I fake an specific browser client when using Java's Net library?
  • How reduce the height of an mschart by breaking up the y-axis
  • javascript inside java/jsp code
  • Perl system calls when running as another user using sudo
  • sending/ receiving email in Java
  • Release, debug version and Authorization Google?
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Android Studio and gradle
  • Proper folder structure for lots of source files
  • Hits per day in Google Big Query
  • How does Linux kernel interrupt the application?
  • Can't mass-assign protected attributes when import data from csv file
  • sending mail using smtp is too slow