开发者

How to simulate a Date comparison with String representing dd/mm/yyyy in J2ME?

开发者 https://www.devze.com 2023-04-13 06:30 出处:网络
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 d

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 ?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消