How to parse a date

0 votes

I am trying to parse this date with SimpleDateFormat and it is not working:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Formaterclass {
    public static void main(String[] args) throws ParseException{
        String strDate = "Thu Jun 18 20:56:02 EDT 2009";
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date dateStr = formatter.parse(strDate);
        String formattedDate = formatter.format(dateStr);
        System.out.println("yyyy-MM-dd date is ==>"+formattedDate);
        Date date1 = formatter.parse(formattedDate);

        formatter = new SimpleDateFormat("dd-MMM-yyyy");
        formattedDate = formatter.format(date1);
        System.out.println("dd-MMM-yyyy date is ==>"+formattedDate);
    }
}
Oct 4, 2018 in Java by Daisy
• 8,120 points
625 views

2 answers to this question.

0 votes

You cannot expect to parse a date with a SimpleDateFormat that is set up with a different format.

To parse your "Thu Jun 18 20:56:02 EDT 2009" date string you need a SimpleDateFormat like this (roughly):

SimpleDateFormat parser=new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");

Use this to parse the string into a Date, and then your other SimpleDateFormat to turn that Date into the format you want.

        String input = "Thu Jun 18 20:56:02 EDT 2009";
        SimpleDateFormat parser = new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");
        Date date = parser.parse(input);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String formattedDate = formatter.format(date);
answered Oct 4, 2018 by Parth
• 4,630 points
0 votes
SimpleDateFormat parser=new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");
String ip = "Thu Jun 18 20:56:02 EDT 2009";
SimpleDateFormat date_parser = new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");
Date date = date_parser.parse(ip);
SimpleDateFormat format_date = new SimpleDateFormat("yyyy-MM-dd");
String formatted_Date = format_date.format(date);

        ...
answered Dec 6, 2018 by Sushmita
• 6,910 points

Related Questions In Java

0 votes
1 answer

How can one day be added to a date?

One possible solution could be using calendar ...READ MORE

answered Jun 8, 2018 in Java by Daisy
• 8,120 points
441 views
0 votes
1 answer

How to parse date string to Date?

The pattern is wrong. You have a ...READ MORE

answered Oct 29, 2018 in Java by Daisy
• 8,120 points
5,536 views
0 votes
1 answer

How to convert a date string to a DateTime object using Joda Time library?

DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss"); DateTime dt = ...READ MORE

answered Jan 9, 2019 in Java by Daisy
• 8,120 points
1,877 views
+5 votes
4 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,450 points

edited Nov 7, 2018 by Omkar 79,299 views
0 votes
3 answers

Increment the date in Java by 1-day

import java.time.LocalDate; public class DateIncrementer { static ...READ MORE

answered Aug 1, 2018 in Java by Akrati
• 3,190 points
4,223 views
0 votes
1 answer

How to calculate the difference between two date instances in Java?

You can use Joda Time Library. Interval i ...READ MORE

answered May 4, 2018 in Java by Parth
• 4,630 points
755 views
0 votes
2 answers

Fetch list of in-between dates using Java

java.time Package The new java.time.package in Java 8 incorporates ...READ MORE

answered Aug 21, 2019 in Java by Sirajul
• 59,230 points
5,359 views
0 votes
1 answer

Convert Milliseconds to “X mins, x seconds” in Java?

I wrote a function which converts milliseconds ...READ MORE

answered May 30, 2018 in Java by Rishabh
• 3,620 points
2,836 views
0 votes
3 answers

How to read a Text File in Java?

You can use readAllLines and the join method to ...READ MORE

answered Jul 28, 2018 in Java by samarth295
• 2,220 points
2,097 views
0 votes
2 answers

How to test that an array contains a certain value?

public static final String[] VALUES = new ...READ MORE

answered Jul 17, 2018 in Java by Sushmita
• 6,910 points
807 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP