×

Discussion Board

Results 1 to 12 of 12
  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    11

    Timestamp and Date

    Hi,
    Can i get the Date and Time from a given Timestamp in J2ME?
    Thanks for help

    goonii

  2. #2
    Super Contributor
    Join Date
    Nov 2003
    Location
    Bangalore , India
    Posts
    4,429

    Re: Timestamp and Date

    Hi goonii! Welcome to Nokia Discussion Boards!!

    Please go through the API Docs of java.util.Date Class.

    Also System.currentTimeMillis();

    Hope this helps!

    Regards
    Gopal

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    11

    Re: Timestamp and Date

    Hi Gopal,

    currentTimeMillis() returns the current time in milliseconds.
    But i need the Date (and Time) in Date or string format converted from a Timestamp!

    Like: 1149593410 ----> Tuesday, June 6, 2006 - 13:30:10

  4. #4
    Registered User
    Join Date
    Dec 2005
    Location
    Brazil
    Posts
    1,884

    Re: Timestamp and Date

    Hi goonii,

    Try the code snippet below:

    long dtMili = System.currentTimeMillis();
    Date dt = new Date(dtMili);
    Calendar cal = Calendar.getInstance();
    cal.setTime(dt);
    int day = cal.get(Calendar.DAY_OF_MONTH);
    int month = cal.get(Calendar.MONTH);
    int year = cal.get(Calendar.YEAR);

    Is it what you're looking for? Hope it helps!

    BR,

    Juarez Junior

  5. #5
    Registered User
    Join Date
    Jun 2006
    Posts
    11

    Post Re: Timestamp and Date

    Hi Juarez Junior!
    No!! :-)
    I have a timestamp (the milliseconds since January 1, 1970, 00:00:00 GMT.)!
    My timestamp is: 1149593410
    Now i try to convert him in readable Format!
    Nothing more ist to do!! :-)
    Last edited by goonii; 2006-06-06 at 13:18.

  6. #6
    Super Contributor
    Join Date
    Nov 2003
    Location
    Bangalore , India
    Posts
    4,429

    Re: Timestamp and Date

    In case you have missed, please give a try with toString()
    It converts this Date object to a String of the form:
    dow mon dd hh:mm:ss zzz yyyy where:

    dow is the day of the week (Sun, Mon, Tue, Wed, Thu, Fri, Sat).

    mon is the month (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec).

    dd is the day of the month (01 through 31), as two decimal digits.

    hh is the hour of the day (00 through 23), as two decimal digits.

    mm is the minute within the hour (00 through 59), as two decimal digits.

    ss is the second within the minute (00 through 61, as two decimal digits.

    zzz is the time zone (and may reflect daylight savings time). If time zone information is not available, then zzz is empty - that is, it consists of no characters at all.

    yyyy is the year, as four decimal digits.

    Ref: http://www.it.iitb.ac.in/~it601/reso...tml#toString()

    Please let us know whether this helps or not!

    Regards
    Gopal

  7. #7
    Registered User
    Join Date
    Jun 2006
    Posts
    11

    Re: Timestamp and Date

    NO!!
    I do not have a Date or Date object or something similar!
    I ONLY HAVE A TIMESTAMP!! (1149593410)
    Now i try to convert him in human readable format!
    Last edited by goonii; 2006-06-06 at 18:19.

  8. #8
    Registered User
    Join Date
    Dec 2005
    Location
    Brazil
    Posts
    1,884

    Re: Timestamp and Date

    Hi goonii,

    This method call - System.currentTimeMillis() - returns a long that's that same timestamp you're talking about. Note that it's a primitive type (not object) so it's a raw type and you cannot get more from it in its state. You'll need the Date object anyway and Gopal's approach is good.

    BR,

    Juarez Junior

  9. #9
    Registered User
    Join Date
    Jun 2006
    Posts
    11

    Re: Timestamp and Date

    Ok, please take a look at my testcode:


    long cMillis = System.currentTimeMillis();
    Date sDate = new Date(cMillis);
    dateField2 = new DateField("", DateField.DATE_TIME);
    dateField2.setDate(sDate);

    This Code works fine and returns me the actual date and time.


    Ok, and now this code:

    long cMillis = 1149593410; // <-- a timestamp
    Date sDate = new Date(cMillis);
    dateField2 = new DateField("", DateField.DATE_TIME);
    dateField2.setDate(sDate);

    returns me 07:19:00 14.01.19170

    but the right time/date of this timestamp ist 13:30:10 - 06.06.2006



    System.out.println(System.currentTimeMillis());
    returns me: 1149627088470


    So i tried:
    long cMillis = 1149627088470;
    Date sDate = new Date(cMillis);
    dateField2 = new DateField("", DateField.DATE_TIME);
    dateField2.setDate(sDate);

    retruns "integer number too large: 1149627088470"

    I do not have any idea, what i can do.

    Can you give me a bit of code?

  10. #10
    Registered User
    Join Date
    Jun 2006
    Posts
    11

    Re: Timestamp and Date

    I need your help please! How can i convert my timestamp in human readable format?

    Regards goonii

  11. #11
    Registered User
    Join Date
    Jun 2006
    Posts
    11

    Re: Timestamp and Date

    I need your help please! How can i convert my timestamp in human readable format?

    Regards goonii

  12. #12
    Registered User
    Join Date
    Jun 2007
    Location
    Tampere, Finland
    Posts
    2

    Cool Re: Timestamp and Date

    My code for this:

    Calendar calendar = Calendar.getInstance();
    calendar.setTime(new Date(timeStamp));
    StringBuffer temp = new StringBuffer();

    temp.append(calendar.get(Calendar.DAY_OF_MONTH));
    temp.append('.');
    temp.append(calendar.get(Calendar.MONTH) + 1);
    temp.append('.');
    temp.append(calendar.get(Calendar.YEAR));

    temp.append(' ');
    temp.append(calendar.get(Calendar.HOUR_OF_DAY));
    temp.append(':');
    temp.append(calendar.get(Calendar.MINUTE));
    temp.append(':');
    temp.append(calendar.get(Calendar.SECOND));

    return temp.toString();

    You can change the order of fields. I hope this helps.
    Br, Tamas

Similar Threads

  1. Sms date and time
    By zafalimited in forum Mobile Java General
    Replies: 3
    Last Post: 2006-04-26, 02:13
  2. SMS timestamp
    By ivanyiu in forum General Messaging
    Replies: 0
    Last Post: 2006-02-07, 06:21
  3. Timestamp of Contacts
    By superawx in forum PC Suite API and PC Connectivity SDK
    Replies: 1
    Last Post: 2005-01-21, 08:39
  4. TimeStamp Property of IContact2
    By superawx in forum PC Suite API and PC Connectivity SDK
    Replies: 0
    Last Post: 2004-07-21, 16:24
  5. MMS Date(unix timestamp) encoding - q?
    By kvn15 in forum General Messaging
    Replies: 1
    Last Post: 2003-05-28, 10:16

Posting Permissions

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