如何找出j2me中的当前日期时间戳?

时间:2011-01-27 09:46:35

标签: java java-me timestamp midp

如何在j2me代码中获取当前日期的时间戳?

我想将当前日期转换为时间戳,就像我们在Java SE中所做的那样:

java.util.Date date= new java.util.Date();
System.out.println(new Timestamp(date.getTime()));

4 个答案:

答案 0 :(得分:3)

你走了:

public class DateAndTime extends MIDlet implements CommandListener{
  private Display disp;
  private Date d;
  private DateField currentDate;
  private Form form;
  private int index;

  public DateAndTime(){
    form = new Form("Data and Time");
    d = new Date();
    currentDate = new DateField("", DateField.DATE_TIME);
    currentDate.setDate(d);
  }

  public void startApp(){
    index = form.append(currentDate);
    disp = Display.getDisplay(this);
    disp.setCurrent(form);
  }

}

此链接提供完整的教程:current-date-time

希望这有帮助。

修改: -

尝试这样的事情:

new Date().getTime();
//------- OR
long dtMili = System.currentTimeMillis();
Date dt = new Date(dtMili);
Calendar cal = Calendar.getInstance();
cal.setTime(dt);
cal.getTime(); //-------- This will give you the timestamp.

答案 1 :(得分:2)

new java.util.Date();

答案 2 :(得分:1)

使用java.util package

请参阅此链接

http://www.roseindia.net/j2me/current-date-time.shtml

答案 3 :(得分:0)

时间= Unix时间戳格式。我添加了64800秒,因此它将被转换为Mountain Standard Time。

*timestamp 24 * 60 * 60

.getTime()//milliseconds 24 * 60 * 60 * 1000

private DateField dateField1; ///////////////////////

dateField1= new DateField("Date:", DateField.DATE); f.append(dateField1);

Date d = new Date(); dateField1.setDate(d);

String TimeSeg = String.valueOf(((dateField1.getDate().getTime()/1000)+64800));