Getting Wrong Date When I Am Trying To Parse Actual Date
Solution 1:
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.
DateTimeFormatterf= DateTimeFormatter.ofPattern( "EEEE, MMMM dd HH:mm a" , Locale.US ) ;
Stringinput="Saturday, June 10 11:18 AM" ;
MonthDaymd= MonthDay.parse( input , f ) ;
LocalTimelt= LocalTime.parse( input , f ) ;
Specify a year to get a LocalDate
.
LocalDateld= md.atYear( 2017 );
Specify the time zone intended as the context for this date-time.
ZoneIdz= ZoneId.of( "America/Montreal" ) ;
ZonedDateTimezdt= ZonedDateTime.of( ld , lt , z ) ;
Solution 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.
StringactualDateString="Saturday, June 10 11:18 AM";
DateTimeFormatterformatter= DateTimeFormatter.ofPattern("EEEE, MMMM dd HH:mm a", Locale.ENGLISH);
// Step one, porseTemporalAccessorparsed= formatter.parse(actualDateString);
// Use day of week, month and day and find a year that matchesDayOfWeekactualDayOfWeek= DayOfWeek.from(parsed);
MonthDayactualMonthDay= MonthDay.from(parsed);
intcancidateYear= Year.now(ZoneId.systemDefault()).getValue();
while (! actualMonthDay.atYear(cancidateYear).getDayOfWeek().equals(actualDayOfWeek)) {
cancidateYear--;
}
// Final step, put the date and time together from the piecesLocalDateTimedateTime= 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
Solution 3:
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() +"]");
Solution 4:
Try this, it's working for me:
SimpleDateFormatsdf=newSimpleDateFormat("EEEE, MMMM dd HH:mm aa");
Log.d("date:","date is:"+sdf.format(newjava.util.Date()));
Post a Comment for "Getting Wrong Date When I Am Trying To Parse Actual Date"