1

I have a String Wed, 22 Aug 2012 06:29:31 +0530 like this, now I want to convert this String to a date format and I need to display day an date Wed, 22 Aug 2012 like this and eliminating other remaining String?

I need to display the date on my LWUIT form Screen

String
  • 3,660
  • 10
  • 43
  • 66

5 Answers5

2
String date = "Wed, 22 Aug 2012 06:29:31 +0530";
String shortDate = date.substring(0, 16);
Telmo Pimentel Mota
  • 4,033
  • 16
  • 22
2

You need to write the code manually by using the methods in java.util.Calendar, we did some formatters and localization API's in Codename One so you can look at our code for reference or just move to Codename One.

Shai Almog
  • 51,749
  • 5
  • 35
  • 65
2

For this date formate "date_posted":"2012-04-19T16:45:33+01:00" i use the following code. You can customize it for your needs.

 try {
                String dateString = date_posted;
                String[] dateArray = Util.split(dateString, "T");
                String[] date = Util.split(dateArray[0], "-");
                String[] time = Util.split(dateArray[1], ":");
            Calendar calStart = Calendar.getInstance();
            calStart.set(Calendar.MONTH, Integer.parseInt(date[1]) - 1);
            calStart.set(Calendar.DAY_OF_MONTH, Integer.parseInt(date[2]));
            calStart.set(Calendar.YEAR, Integer.parseInt(date[0]));
            calStart.set(Calendar.HOUR_OF_DAY, Integer.parseInt(time[0]));
            calStart.set(Calendar.MINUTE, Integer.parseInt(time[1]));
            calStart.set(Calendar.MILLISECOND, 0);

            Date postDate = calStart.getTime();

            post_time = postDate.getTime();
        } catch (Exception ex) {
            Logger.err("fromJSON", ex);
        }
Ajibola
  • 1,218
  • 16
  • 28
2

I use something like this :

String getDateString(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);

    int year = calendar.get(Calendar.YEAR);
    int month = calendar.get(Calendar.MONTH);
    int day = calendar.get(Calendar.DAY_OF_MONTH);
    int hour = calendar.get(Calendar.HOUR_OF_DAY);
    int minute = calendar.get(Calendar.MINUTE);
    int second = calendar.get(Calendar.SECOND);

    return new String(/*Any format you need*/);
}
eddy
  • 4,373
  • 16
  • 60
  • 94
1

There is no specific method in j2me to convert string to date. So, that is not possible at all.

Kalai Selvan Ravi
  • 2,846
  • 2
  • 16
  • 28