问题
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.
回答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.
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 ) ;
回答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
- ThreeTenABP
- Question: How to use ThreeTenABP in Android Project
回答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() +"]");
回答4:
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()));
来源:https://stackoverflow.com/questions/44470105/getting-wrong-date-when-i-am-trying-to-parse-actual-date