How to create a field of format dd/yyyy?

2019-08-16 07:51发布

问题:

The field is like a DateField but instead its value is just the month and the year like '05/2011' : how to create such a field ?

回答1:

In java-me, you can use the java.util.Calendar package for formatting the date.

Here is snippet from this tutorial on displaying the date and time in Java ME:

private void outputDateUsingCalendar(Date date) {
   Calendar calendar = Calendar.getInstance();
   calendar.setTime(date);
   StringBuffer sb = new StringBuffer();

   int day = calendar.get(Calendar.DAY_OF_MONTH);
   sb.append(numberToString(day));
   sb.append("-");
   int month = calendar.get(Calendar.MONTH) + 1;
   sb.append(numberToString(month));
   sb.append("-");
   sb.append(calendar.get(Calendar.YEAR));

   StringItem item = new StringItem("Using Calendar", sb.toString());
   mainForm.append(item);
}


标签: java-me