Meaningless Drivel is fun!*
The moose likes Product and Other Certifications and the fly likes Something about current time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Product and Other Certifications
Bookmark "Something about current time" Watch "Something about current time" New topic
Author

Something about current time

Fisher Daniel
Ranch Hand

Joined: Sep 14, 2001
Posts: 582
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

Joined: Feb 09, 2004
Posts: 147
Try: Date.getTime(), and take a look at the Date API.


Alibabra
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
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...


Co-author of SCMAD Exam Guide, Author of JMADPlus
SCJP1.2, CCNA, SCWCD1.4, SCBCD1.3, SCMAD1.0, SCJA1.0, SCJP6.0
Fisher Daniel
Ranch Hand

Joined: Sep 14, 2001
Posts: 582
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

Joined: Jun 08, 2002
Posts: 3178
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

Joined: Sep 14, 2001
Posts: 582
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

Joined: Jun 08, 2002
Posts: 3178
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....
prashanthNair
Greenhorn

Joined: May 13, 2004
Posts: 18
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

Joined: Oct 22, 2004
Posts: 10
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
Vaneet Sharma
Greenhorn

Joined: Oct 22, 2004
Posts: 10
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

Joined: Feb 05, 2003
Posts: 4727

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();


A good workman is known by his tools.
prashanthNair
Greenhorn

Joined: May 13, 2004
Posts: 18
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Something about current time