×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    19

    Calendar and Date format in J2ME

    Hello

    I have to count the days that has passed from 30/12/1899 (0Hrs 0Min 0Sec) (Delphi Date Format) to another Date (with 0Hrs 0Min and 0Sec).
    This is in order to conver the count days between dates as Delphi does (I have an existing software that works this way and I cannot modify it).
    The problems are that hava start to count from 1970, and I don't have the java.text package so to parse and create the calendar. Can anybody give me a hand?

    Thanks
    Ezequiel

  2. #2
    Registered User
    Join Date
    Dec 2005
    Posts
    13

    Re: Calendar and Date format in J2ME

    I am a little unclear as to what you are looking for?

    Are you just looking for a function that will parse a date?

    Example:
    Code:
    public static Date parseDate(String dateString) { ... }
    
    Date d = parseDate("1960-07-31 11:55:00.0")

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    9

    Re: Calendar and Date format in J2ME

    I'm looking for a function to parse a date, do you know one?

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    13

    Re: Calendar and Date format in J2ME

    Here's some code - Let me know if it does the trick!
    /*
    Accepted Format:
    01234567890123456789012
    "YYYY MM DD HH mm ss SSS"
    ex. 1960-07-31 00:00:00.0
    Assumptions:
    - there is at exactly 1 character (any character) between each field
    (which will be ignored during parsing)
    */
    public static Date parseDate(String dateString) throws IllegalArgumentException, DateParseException {
    Date date = new Date(0);
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);

    //System.out.println("Calendar Before "+date);

    if (dateString == null || dateString.equals("")) {
    throw new IllegalArgumentException("Invalid String to Parse as Date - dateString was null or empty");
    }

    int strSize = dateString.length();

    if (strSize < 21) {
    throw new IllegalArgumentException("Invalid String to Parse as Date - dateString invalid string length ("+strSize+")");
    }

    String yearStr = dateString.substring(0,4);
    String monthStr = dateString.substring(5,7);
    String dayStr = dateString.substring(8,10);
    String hourStr = dateString.substring(11,13);
    String minuteStr = dateString.substring(14,16);
    String secondsStr = dateString.substring(17,19);
    String millisStr = dateString.substring(20,Math.min(strSize,23));

    int year = 0;
    int day = 0;
    int month = 0;
    int hour = 0;
    int minute = 0;
    int seconds = 0;
    int millis = 0;

    try {
    year = Integer.parseInt(yearStr);
    } catch (Exception e) {
    throw new DateParseException("Could not parse '"+yearStr+"' as a valid year");
    }
    try {
    day = Integer.parseInt(dayStr);
    } catch (Exception e) {
    throw new DateParseException("Could not parse '"+dayStr+"' as a valid day");
    }
    try {
    month = Integer.parseInt(monthStr) - 1; //Zero Based Months
    } catch (Exception e) {
    throw new DateParseException("Could not parse '"+monthStr+"' as a valid month");
    }

    try {
    hour = Integer.parseInt(hourStr);
    } catch (Exception e) {
    throw new DateParseException("Could not parse '"+hourStr+"' as a valid hour");
    }
    try {
    minute = Integer.parseInt(minuteStr);
    } catch (Exception e) {
    throw new DateParseException("Could not parse '"+minuteStr+"' as a valid minute");
    }
    try {
    seconds = Integer.parseInt(secondsStr);
    } catch (Exception e) {
    throw new DateParseException("Could not parse '"+secondsStr+"' as a valid seconds");
    }
    try {
    millis = Integer.parseInt(millisStr);
    } catch (Exception e) {
    throw new DateParseException("Could not parse '"+millisStr+"' as a valid millis");
    }

    //System.out.println("Y: "+year+" M: "+month+" D: "+day);
    //System.out.println("H: "+hour+" m: "+minute+" s: "+seconds+" S: "+millis);

    cal.set(Calendar.MONTH,month);
    cal.set(Calendar.DATE,day);
    cal.set(Calendar.YEAR,year);
    cal.set(Calendar.HOUR_OF_DAY,hour);
    cal.set(Calendar.MINUTE,minute);
    cal.set(Calendar.SECOND,seconds);

    //System.out.println("Date After "+date);
    date = cal.getTime();

    return date;
    }

Similar Threads

  1. Date Field class in J2ME
    By yasomca in forum Mobile Java General
    Replies: 3
    Last Post: 2005-10-04, 02:23
  2. How to update calendar entrys date?
    By kosjanne in forum Symbian
    Replies: 3
    Last Post: 2005-03-08, 17:08
  3. Date and Calendar
    By s01200994 in forum Mobile Java General
    Replies: 0
    Last Post: 2004-03-04, 06:49
  4. Calendar in J2ME vs. J2SE
    By dennisluemkemann in forum Mobile Java General
    Replies: 0
    Last Post: 2003-02-06, 10:48

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×