This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Something about current time

 
Fisher Daniel
Ranch Hand
Posts: 582
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all,
I have a problem about current time.
In my class, I use Calendar.getInstance() to get current time.
Why does Calendar.getInstance() always show time with GMT time zone?
If I want to get current time on the device, what should I do?

thanks
daniel
 
Alibabra Sanjie
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try: Date.getTime(), and take a look at the Date API.
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Fisher Daniel:
Dear all,
I have a problem about current time.
In my class, I use Calendar.getInstance() to get current time.
Why does Calendar.getInstance() always show time with GMT time zone?
If I want to get current time on the device, what should I do?

thanks
daniel


Try this daniel... Just coded on-the-fly using Sun Java Studio Mobility 6...



Hope this helps...
 
Fisher Daniel
Ranch Hand
Posts: 582
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried to use calendar.getTime(), it produce the same result as I use calendar.getInstance().
And I also try a midlet application about Time from http://java.mob385.com/en/sat.shtml. This application also get GMT time instead of my local time zone.

Does MIDP 2.0 specification say that Calendar or Date object in J2ME behave like that?

thanks
daniel
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Fisher Daniel:
I have tried to use calendar.getTime(), it produce the same result as I use calendar.getInstance().
And I also try a midlet application about Time from http://java.mob385.com/en/sat.shtml. This application also get GMT time instead of my local time zone.

Does MIDP 2.0 specification say that Calendar or Date object in J2ME behave like that?

thanks
daniel

Did you try my application? It's working well on my machine...
 
Fisher Daniel
Ranch Hand
Posts: 582
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your response...

I have tried your code on my machine.. and I still get GMT time.
Actually my local time is GMT +8 and the display shows 8 hours behind my local time.

Are there something wrong with my configuration in my J2ME Wireless Toolkit?

Thanks
daniel
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Fisher Daniel:
Thank you for your response...

I have tried your code on my machine.. and I still get GMT time.
Actually my local time is GMT +8 and the display shows 8 hours behind my local time.

Are there something wrong with my configuration in my J2ME Wireless Toolkit?

Thanks
daniel


Then can you show the result that you get? You may print your screen and post here...

Hope we might be able to help you....
 
Prashanth Nair
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try Calendar.getInstance() which gets the current time including time zone settings, which the default constructor of Date does not do

Calendar cal= Calendar.getInstance();
System.out.println( "time: " + cal.get(cal.HOUR_OF_DAY) + ":" + cal.get(cal.MINUTE) );

Use the above code
For getting a full current date i used the following workaround
(optimize it further )
-----------------------------------------------------------------

Date date =new Date();
StringBuffer sb1 =new StringBuffer(30);
Calendar cd = Calendar.getInstance();
Calendar ct = Calendar.getInstance();
ct.set( Calendar.MONTH,cd.get( Calendar.MONTH ) );
ct.set( Calendar.DAY_OF_MONTH,cd.get( Calendar.DAY_OF_MONTH ) );
ct.set( Calendar.YEAR,cd.get( Calendar.YEAR ) );
date=ct.getTime();
String temp = date.toString().substring(0,11);
sb1.append(temp);
sb1.append(cd.get(cd.HOUR_OF_DAY));
sb1.append(":");
sb1.append(cd.get(cd.MINUTE));
temp = sb1.toString();
return temp;

------------------------------------

You will get the current date and time correctly with this

Happy coding.....
Mandrake
 
Vaneet Sharma
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone tell me why i cannot get the localised time in my midlet

check this code and tell me why i cannot get the Local time on my midlet

public void addDateTime() {

//Getting Timezone
TimeZone timezone = TimeZone.getTimeZone("GMT+01:00");
//Calendar c = Calendar.getInstance(timezone);

//Creating today's time
currentTime = new Date();
System.out.println("Date objec: " + currentTime.getTime());

//puting timezone object in DateField constructor
dateTimeAlert = new DateField("Use Format Defined", DateField.DATE_TIME,timezone);
dateTimeAlert.setDate(currentTime);

//adding date to the form
this.append(dateTimeAlert);

this.addCommand(setTimer);
this.addCommand(resetTimer);
this.addCommand(exit);

}

any help will be appreciated
Thankx
Vaneet
 
Vaneet Sharma
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mandrake , the magician ( Greenhorn),
Can u help me to solve the problem of getting current DATE


the code you gave on website is not working.
im adding the date object on DATEFIELD.
pls let me know

Thankx
Vaneet
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vaneet,

Your Date object is not coming from the Calendar object which holds the time zone.

Instead of:
currentTime = new Date();
try:
currentTime = c.getTime();
 
Prashanth Nair
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vaneet try this code for DateField

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;

public class TestScreen extends MIDlet
{
public Form frm1;
public Display display;
public DateField dateField;

public TestScreen()
{
frm1 = new Form("Sample");
display=Display.getDisplay(this);
}


public void startApp()
{
getDate();
showForm();
}

public void pauseApp()
{
}


public void showForm()
{
dateField =new DateField("Today",DateField.DATE_TIME);
dateField.setDate(getDate());
frm1.append(dateField);
display.setCurrent(frm1);
}

public Date getDate()
{
Date date =new Date();
Calendar cd = Calendar.getInstance();
Calendar ct = Calendar.getInstance();
ct.set( Calendar.MONTH,cd.get( Calendar.MONTH ) );
ct.set( Calendar.DAY_OF_MONTH,cd.get( Calendar.DAY_OF_MONTH ) );
ct.set( Calendar.YEAR,cd.get( Calendar.YEAR ) );
ct.set(Calendar.HOUR_OF_DAY,cd.get(Calendar.HOUR_OF_DAY));
ct.set(Calendar.MINUTE,cd.get(Calendar.MINUTE));
date=ct.getTime();
return date;
}

public void destroyApp(boolean unconditional)
{
}

}

-------------------------

mandrake
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic