当我尝试解析实际日期时出现错误的日期 [英] Getting wrong date when I am trying to parse actual date

查看:134
本文介绍了当我尝试解析实际日期时出现错误的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的实际日期格式为:


6月10日星期六上午11:18

Saturday, June 10 11:18 AM

我想将此格式转换为 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));

该行的输出为:


1970年6月10日星期三00:00:00 GMT + 05:30

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.

推荐答案

总是在有人问有关日期和/或时间的问题时Android,我鼓励您查看ThreeTenABP库。它提供了适用于Android的现代Java日期和时间类。这些通常比过时的 Date SimpleDateFormat Calendar

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.

在您的情况下,我会做更多。你没有一年。您可以使用效果不佳的旧 GregorianCalendar 来设置年份,但是如果要选择的年份要求变得有点复杂,则您最好使用现代阶级。这是一个示例,在该示例中,我找到了字符串中星期几与星期几匹配的最新年份,仅作为示例,因为这可能与您的确切要求不符。我首先将字符串解析为 TemporalAccessor ,我认为这是最终创建所需对象的中间形式。从中,我选取星期,月份和日期,并找到适用的年份。从这些片段中我们得出一个日期,从解析结果中我们得出时间(11:18),并建立 LocalDateTime 这是最终结果。

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")));

此打印:

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.

感谢雨果(Hugo)帮助我简化一次(再次)。

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

  • ThreeTenABP
  • Question: How to use ThreeTenABP in Android Project

这篇关于当我尝试解析实际日期时出现错误的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆