Problems with Calendar class in J2ME.

Posted on 2007-07-25
Last Modified: 2013-12-29
I developing an application that runs on cellphones. I'm using the last version of the Sun Java Wirelless Toolkit (Version 2.5). I need to take the date and the hour at an specific moment so I got the following code to do this.

        calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT-05:00"));
        calendar.setTime(new Date());

        // to convert the current date to an String
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DATE);
        date = (month<10? "0": "")+ month+"/"+(day<10? "0": "") + day + "/" + (year<10? "0": "")+year;

        // to convert the current time to an String
        String[] AMPM = {"AM", "PM"};
        int hour = calendar.get(Calendar.HOUR);
        int minute = calendar.get(Calendar.MINUTE);
        String am_pm = (AMPM[calendar.get(Calendar.AM_PM)]);
        time = (hour + ":" + minute + am_pm);

The problem is that this piece of code works well for the Date but for the time it doesn't. It gives me one hour late that the actual one.  Another problem that I have also with the time is that when for intance is 2:07 it displays 2:7 without the zero.  If there is a way to to this in another and easy way please I will appreciate any code. Thank
Question by:jonathan_otero
    LVL 3

    Accepted Solution

    Are you taking into account Daylight Savings Time?  Most people in GMT -05:00 are on Daylight Savings Time right now, which means that they have set their clocks to be one hour ahead of what would seem like the actual hour to someone not on Daylight Savings TIme.
    As for the 2:7 problem, by the code you posted it seems you simply forgot to check if the minute was less than 10 and if so put out an extra zero (like you did for the date).
    time = (hour + ":" + (minute<10?"0":"") + minute + am_pm)
    LVL 4

    Expert Comment

    just take out the timezone and your time would be correct

    calendar = Calendar.getInstance();

    As for the 2:7 problem, i think DefreeComan code is the shortest way to do it.


    LVL 4

    Assisted Solution

    import java.text.DateFormat;

    Date d = new Date();
    DateFormat df1 = DateFormat.getDateInstance(DateFormat.DATE_FIELD);
    DateFormat df2 = DateFormat.getTimeInstance(DateFormat.SHORT);
    String s1 = df1.format(d);
    String s2 = df2.format(d);
    System.out.println("Date in dd/MM/yy format > " + s1);
    System.out.println("Time in hh:mm format > " + s2);

    I found this  shorter way, but the date format is fixed to dd/MM/yy, can't get you the yyyy format.

    :) Hope this help

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    If you haven’t already, I encourage you to read the first article ( in my series to gain a basic foundation of R and R Studio.  You will also find the …
    Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
    The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
    The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now