如何在j2me代码中获取当前日期的时间戳?
我想将当前日期转换为时间戳,就像我们在Java SE中所做的那样:
java.util.Date date= new java.util.Date();
System.out.println(new Timestamp(date.getTime()));
答案 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)
答案 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));