J2ME: Get only year / month / day with DateField

I am trying to use dateField to display which year, month or day were selected on the calendar.

public DateField getDateField() {
dateField = new DateField("dateField", DateField.DATE);
dateField.setDate(new java.util.Date(System.currentTimeMillis()));
}

Then I would like to use this code, but instead of the long date "Thu Oct 07 00:00:00 GMT + 02: 00 2010" I would just like to see which month was selected, for example, "October" or "10" and so on. .d.

String month = dateField.getDate().toString();
System.out.println("Selected month: " + month);

I cannot find anything good on this date. I want to get a short date, for example, "2010-10-05" if that date was selected in the date field.

+3
source share
1 answer
Calendar cal= Calendar.getInstance();
cal.setTime(dateField.getDate());
String date = cal.get(Calendar.YEAR) + "-" + ( cal.get(Calendar.MONTH) + 1 ) + "-" + cal.get(Calendar.DAY_OF_MONTH);
+8
source

Source: https://habr.com/ru/post/1768434/


All Articles