1

I am trying to parse string into a date using the following code:

public static Date dateFormatter(String s)
{
    SimpleDateFormat ft = new SimpleDateFormat ("MMddYYYY"); 
    Date excelDate=null;
    try
    {
        excelDate = ft.parse(s);    
        Date formatString = ft.format(excelDate);
        System.out.println("Date to be printed in Excel is :" +formatString);
        return excelDate;
    }
    catch(Exception ae)
    {
        System.out.println("No date");
    }
    return excelDate;
}

I am passing in the argument "04202017".

This function is not working for me. I am not able to figure out what I am doing wrong. Can anybody please help me?

khelwood
  • 55,782
  • 14
  • 81
  • 108
Rudrika
  • 49
  • 3
  • 3
    Try `"MMddyyyy"` . – Arnaud Mar 09 '17 at 10:54
  • [`format`](https://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html#format(java.util.Date)) is not [`parse`](https://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html#parse(java.lang.String)) – khelwood Mar 09 '17 at 10:58
  • Why do you format the parse result ? (into the wrong type) – AxelH Mar 09 '17 at 11:00

5 Answers5

3

You have to use ft.parse(s); instead of format(excelDate). Format is the other way (Date -> String)

DateFormat.parse(String)

And you dont have to parse the Date back to a String.

Corrected code:

public static Date dateFormatter(String s) {
    SimpleDateFormat ft = new SimpleDateFormat ("MMddYYYY"); 
    Date excelDate = null;
    try {
        excelDate = ft.parse(s);    
        System.out.println("Date to be printed in Excel is :" +excelDate);
        return excelDate;
    } catch(Exception ae) {
        System.out.println("No date");
    }
    return excelDate;
}
Ocean15
  • 135
  • 9
  • YYYY is not a valid decleration for year i think. It should be MMddyyyy or something like that. – ReadyFreddy Mar 09 '17 at 11:06
  • OP is already doing that, but he is trying to reformat the `excelDate `, into a Date ( instead of a String). So this answer is not correct (yet ;) ) PS : _You have to use `ft.parse(excelDate); `_, you means `parse(s)` ? Because you can't pass a Date into `parse` – AxelH Mar 09 '17 at 11:08
  • 1
    @AxelH I corrected my answer – Ocean15 Mar 09 '17 at 11:16
  • Yeah i am trying to reformat the the string 04202017 into 2017-04-20.. I was trying to write a function which takes string as as an argument and returns date in format of YYYY-MM-DD – Rudrika Mar 10 '17 at 05:33
1

Try parse method instead of format

For String to Date, use:

SimpleDateFormat.parse(String);

For Date to String, use:

SimpleDateFormat.format(date);

However, in your code, you already parsed the String and assigned into excelDate on this line:

excelDate = ft.parse(s);
Mehmood Memon
  • 1,129
  • 2
  • 12
  • 20
1

It would be nice to use Java 1.8's new time classes (which are in java.time.* package).

public static void main(String[] args)
{        
    LocalDateTime dateTime = LocalDateTime.now();
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    // To String
    String dateString = dateTime.format(formatter);
    System.out.println(dateString);

    // To LocalDateTime
    LocalDateTime parsedLocalDateTime = LocalDateTime.parse(dateString, formatter);
}
user218046
  • 623
  • 6
  • 20
  • This is only formatting the current date, you should include the way to parse a String into a `LocalDateTime` in your answer to be correct. Since this is the opposite ;) – AxelH Mar 09 '17 at 11:14
  • Oh yeah, you are right. I've edited it. – user218046 Mar 09 '17 at 11:17
1

You already parsed String s to excelDate with date format that you want. So i think it's good and enough to print just excelDate.

System.out.println("Date to be printed in Excel is :" +excelDate);

Like that.

And also change MMddYYYY to MMddyyyy.

ReadyFreddy
  • 898
  • 8
  • 20
1

try this one:

    String string = "march 9, 2017";
    DateFormat format = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH);
    Date date = format.parse(string);
    System.out.println(date); 
MurugananthamS
  • 2,395
  • 4
  • 20
  • 49