如何在 J2ME 中模拟与表示 dd/mm/yyyy 的字符串进行日期比较?

发布于 2024-12-09 19:09:38 字数 134 浏览 0 评论 0原文

csv字符串数据中有一个Date-like String数据,即Date-like String数据的形式为dd/mm/yyyy。我想实现一个比较两个类似日期的字符串数据的方法,就像比较两个日期对象一样。在J2ME中如何实现呢?

There is a Date-like String data from a csv String data, that is the Date-like String data has a form of dd/mm/yyyy. I want to implement a method which compares two Date-like String data like if I compare two Date objects. How to achieve that in J2ME ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如果没有 2024-12-16 19:09:38

您可以通过首先比较单个子字符串来比较字符串,如下所示:

public int compare(String date1, String date2) {
  int year = date1.substring(6).compareTo(date2.substring(6));
  if (year == 0) {
     int month = date1.substring(3,5).compareTo(date2.substring(3,5));
     if (month == 0) {
       return date1.substring(0,2).compareTo(date2.substring(0,2));
     } else {
       return month;
     }
  } else {
     return year;
  }
}

即使在 MIDP 中,这也有效,尽管我同意,但这不是赢得任何选美比赛的解决方案。

You can compare the Strings by comparing the individual substrings year-first like there:

public int compare(String date1, String date2) {
  int year = date1.substring(6).compareTo(date2.substring(6));
  if (year == 0) {
     int month = date1.substring(3,5).compareTo(date2.substring(3,5));
     if (month == 0) {
       return date1.substring(0,2).compareTo(date2.substring(0,2));
     } else {
       return month;
     }
  } else {
     return year;
  }
}

This works even in MIDP, although I would agree, it is not the solution winning any beauty-contest.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文