0

My actual date format is:

Saturday, June 10 11:18 AM

I want to convert this format to yyyy-MM-dd HH:mm:ss.

For that I am trying to parse input string using below code:

SimpleDateFormat format = new SimpleDateFormat("EEEE, MMMM dd HH:mm aa", Locale.ENGLISH);
Log.d("date:","date is:"+format.parse(resultstr));

The output of the line gives:

Wed Jun 10 00:00:00 GMT+05:30 1970

But I don't know why it is giving this value instead of actual value.

malli
  • 642
  • 2
  • 12
  • 30
  • 2
    You're not giving it a year, how would it know the year? In fact, it's impossible to know what year you're looking for based on your current code. – elPastor Jun 10 '17 at 06:07
  • 3
    one problem I can see is that your formatter has no way of knowing what year your date is in, so the matching of the week day with the day/month is going to be a problem – njzk2 Jun 10 '17 at 06:08
  • no first i need to parse actual date after that i will see about year.. – malli Jun 10 '17 at 06:08
  • thanks for your valuable suggestion,let me check with year once.. – malli Jun 10 '17 at 06:10
  • 1
    Saturday June 10 only exist once every 7 years (give or take with leap years) – njzk2 Jun 10 '17 at 06:13
  • 1
    Possible duplicate of [How do I simply parse a date without a year specified?](https://stackoverflow.com/questions/8523886/how-do-i-simply-parse-a-date-without-a-year-specified) – Ole V.V. Jun 10 '17 at 06:30
  • 1
    What do you mean by “see about the year”? Of course it can be done if you know the requirements for it. On the other hand, we cannot guide you when we don’t know the requirements. – Ole V.V. Jun 10 '17 at 06:41
  • 2
    @cricket_007 Actually, you *can* indeed "see about the year later" — parse the input as a `MonthDay` sans year. See [my Answer below](https://stackoverflow.com/a/44470607/642706). – Basil Bourque Jun 10 '17 at 07:08

4 Answers4

2

java.time.MonthDay

You might be able to parse the date portion as a MonthDay object, and the time portion as a LocalTime.

I've not tried this code but it might work.

DateTimeFormatter f = DateTimeFormatter.ofPattern( "EEEE, MMMM dd HH:mm a" , Locale.US ) ;
String input = "Saturday, June 10 11:18 AM" ;
MonthDay md = MonthDay.parse( input , f ) ;
LocalTime lt = LocalTime.parse( input , f ) ;

Specify a year to get a LocalDate.

LocalDate ld = md.atYear( 2017 );

Specify the time zone intended as the context for this date-time.

ZoneId z = ZoneId.of( "America/Montreal" ) ;
ZonedDateTime zdt = ZonedDateTime.of( ld , lt , z ) ;
Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
2

Always when someone asks a question about dates and/or times in Android, I encourage to check out the ThreeTenABP library. It provides the modern Java date and time classes for Android. These are generally much more programmer friendly than the outdated Date, SimpleDateFormat and Calendar.

I do so even more in your case. You don’t have a year. You may use the not-good old GregorianCalendar for setting a year, but if the requirements for which year to pick are getting just a little bit complex, you will prefer to work with the modern classes. Here is an example where I find the latest year where the day of week matches the day of week in the string, just as an example, since this probably does not match your exact requirements. I first parse the string into a TemporalAccessor, I think of this as an intermediate form for creating the object/s we want in the end. From this I take the day of week, month and day and find the year that works. From these pieces we put together a date, and from the parse result we take the time (11:18) and build the LocalDateTime that is the final result.

    String actualDateString = "Saturday, June 10 11:18 AM";
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEEE, MMMM dd HH:mm a", Locale.ENGLISH);

    // Step one, porse
    TemporalAccessor parsed = formatter.parse(actualDateString);
    // Use day of week, month and day and find a year that matches
    DayOfWeek actualDayOfWeek = DayOfWeek.from(parsed);
    MonthDay actualMonthDay = MonthDay.from(parsed);
    int cancidateYear = Year.now(ZoneId.systemDefault()).getValue();
    while (! actualMonthDay.atYear(cancidateYear).getDayOfWeek().equals(actualDayOfWeek)) {
        cancidateYear--;
    }

    // Final step, put the date and time together from the pieces
    LocalDateTime dateTime = LocalDateTime.of(actualMonthDay.atYear(cancidateYear), 
                                              LocalTime.from(parsed));

    System.out.println(dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

This prints:

2017-06-10 11:18:00

If instead you want the next year where the day of week matches, just count up instead of down in the loop. If needed the modern classes lend themselves well to more elaborate algorithms for determining the year.

Thanks, Hugo, for helping me simplify (once again).

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • Good! But actually you don't need to create a second parser. You could just do: `LocalDateTime.of(actualMonthDay.atYear(cancidateYear), LocalTime.from(parsed))` –  Jun 10 '17 at 11:28
  • 1
    Thanks, @Hugo! I was thinking something like that should be possible, but didn’t arrive at completing it. – Ole V.V. Jun 10 '17 at 11:30
0

As mentioned in the comments, you are missing a year value, so you will get the default which is 1970 (unix epoch value). You can use Calendar to set a year after parsing.

Calendar cal = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("EEEE, MMMM dd HH:mm aa", Locale.ENGLISH);
cal.setTime(format.parse("Saturday, June 10 11:18 AM"));
cal.set(Calendar.YEAR, 2017);
System.out.println("date is ["+cal.getTime() +"]");
Scott
  • 86
  • 4
0

Try this, it's working for me:

        SimpleDateFormat sdf = new SimpleDateFormat("EEEE, MMMM dd HH:mm aa");
        Log.d("date:","date is:"+sdf.format(new java.util.Date()));
Raj
  • 477
  • 5
  • 20
  • Please explain how it will help. Code only answers are seldom helpful. – Ole V.V. Jun 10 '17 at 06:59
  • 1
    If the asker by “actual date” meant “today’s date”, it does give that (only not in the wanted format of yyyy-MM-dd HH:mm:ss). I for my part understood the question as how to get the date (and time) out of a string in the mentioned format (today’s date being only an example). – Ole V.V. Jun 10 '17 at 07:23
  • 1
    Output on my computer: `date is:lørdag, juni 10 09:17 AM`. I recommend you specify explicit locale. – Ole V.V. Jun 10 '17 at 07:25