【问题标题】:How to split the full date format in date and time?如何在日期和时间中拆分完整的日期格式?
【发布时间】:2016-05-23 11:58:13
【问题描述】:

我有很多我必须解析的示例格式的字符串。我正在尝试确定今天是哪些字符串。

我的问题是,时间快到了,我只需要比较那个日期。

接下来我想用 .after 和 .before 检查时间是否在两个时间戳“HH:mm:ss”之间,但问题是日期快到了。

如何将解析后的格式拆分为日期和时间,以便以自己的方式处理?

如果相关的话,我正在 Android Studio 中工作。

String dtStart = "2016-05-23 07:24:59";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

try {

     if (new Date().equals(format.parse(dtStart)) ) System.out.println("true");
     else System.out.println("false");

     list.add(new LatLng(lat, lng));

} catch (java.text.ParseException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
}

【问题讨论】:

  • 第一次用" "(空格)分割。取第一个元素并再次与- 拆分,取第二个元素并与: 拆分。现在你已经隔离了元素。
  • hehe- 是的,字符串拆分很清楚,但日期函数可能有更简单的方法..
  • 好吧,如果你想通过 api 实现,那么 java 8 对你来说是个好东西。

标签: java date time split


【解决方案1】:

java.time

使用 Java 8 及更高版本中内置的 java.time 类。

大部分功能已在ThreeTen-Backport 中向后移植到 Java 6 和 7,并在 ThreeTen-ABP 中进一步适应 Android。

String dateToParse = "2016-05-23 07:24:59";
LocalDateTime dateTime = LocalDateTime.parse(dateToParse, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
LocalDate localDate = dateTime.toLocalDate();
LocalTime localTime = dateTime.toLocalTime();
// Compare here to your date & time

【讨论】:

  • @John java.time 的大部分内容被反向移植到 Android。请参阅添加到此答案的链接。只需在您的项目中添加一个 jar。旧的日期时间类真的很麻烦。
【解决方案2】:

您可以像这样使用SimpleDateFormat 轻松实现它:

//Houres - seconds
SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

//Years - days
Date hoursAndMinutes = timeFormat.parse(dtStart);
Date yearsMonthsDays = dateFormat.parse(dtStart);

这样,您只能获得约会的小时、分钟和秒。 然后,您可以只对年份和月份执行相同的操作,然后再进行比较。

【讨论】:

  • java.util.Date 对象不适合存储部分日期或时间(即仅年/月/日或仅小时/分钟/秒)。
  • java.text.ParseException:无法解析的日期:“2016-05-23 07:24:59”(偏移量 4)
【解决方案3】:

为了完整起见,以下是使用 Joda 日期时间库和 toLocalDate()toLocalTime() 方法的方法。

DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
DateTime today = new DateTime();
DateTime start = formatter.parseDateTime(dtStart);
if (today.toLocalDate().compareTo(start.toLocalDate()) != 0) {
    System.out.println("true");
} else {
    System.out.println("false");
}

if (today.toLocalTime().compareTo(start.toLocalTime()) > 0) {
...
}

【讨论】:

  • Joda-Time 团队现在建议改用java.time 框架。见the Answer by Shiv V
  • 我确实看到了这个答案,我只是在 OP 无法使用 Java 8 的情况下提供这个解决方案作为替代方案(即使现在也是一个令人惊讶的普遍问题)。
【解决方案4】:

谢谢帮助,对不起,我忘了说我在 android studio..

我在这里找到了我的解决方案:https://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html

String dtStart = "2016/05/23 07:24:59";
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

try {
      Date dtStartOK = format.parse(dtStart);
      String stringDate = DateFormat.getDateTimeInstance().format(dtStartOK);
      System.out.println(stringDate);

      System.out.println(DateFormat.getDateInstance().format(dtStartOK));
      System.out.println(DateFormat.getTimeInstance().format(dtStartOK));

} catch (ParseException e) {
      //Handle exception here, most of the time you will just log it.
      e.printStackTrace();
}

给我:

23.05.2016 07:24:59

23.05.2016

07:24:59

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-13
    • 1970-01-01
    • 2015-04-04
    相关资源
    最近更新 更多