Search code examples
javajava-metimestampmidp

how to find out the current date time stamp in j2me?


How can I get the time stamp for current date in j2me code?

I want to convert the current date into timestamp something like this we do in Java SE :

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

Solution

  • Here you go:

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

    This link provide complete tutorial: current-date-time

    Hope this helps.

    Edit:-

    try something like this:

    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.