I have a problem changing the current time into milliseconds, the milliseconds shown one month before the set date and time. I know the computer starts the months at 0, how can I solve the problem? First, I transfer the date and the time into String
, then I convert the String
into date in SimpleDateFormat
, and then I convert the date into Long
.
For example: When the user enter the date "2018/2/14 11:18 "(AM), the date convert to long is "1515899940000" .
Here is my code:
private void setDateField() {
Calendar c = Calendar.getInstance();
int yy = c.get(Calendar.YEAR);
int mm = c.get(Calendar.MONTH);
int dd = c.get(Calendar.DAY_OF_MONTH);
c.set(Calendar.MONTH, mm);
c.set(Calendar.DAY_OF_MONTH, dd);
c.set(Calendar.YEAR, yy);
DatePickerDialog datePickerDialog = new DatePickerDialog(this,android.R.style.Theme_Holo_Light_Dialog ,new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int selectedYear, int selectedMonth, int selectedDate) {
year = selectedYear;
month = selectedMonth;
day = selectedDate;
date_time = year + "/" + (month + 1) + "/" + day;
timePicker();
}
},year, month, day);
datePickerDialog.getDatePicker().setMinDate(c.getTimeInMillis());
datePickerDialog.show();
}
private void timePicker(){
// Get Current Time
final Calendar c = Calendar.getInstance();
hours = c.get(Calendar.HOUR_OF_DAY);
minutes = c.get(Calendar.MINUTE);
// Launch Time Picker Dialog
TimePickerDialog timePickerDialog = new TimePickerDialog(this,android.R.style.Theme_Holo_Light_Dialog,new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay,int minute) {
hours = hourOfDay;
minutes = minute;
string_date = date_time+" "+format(hours) + ":" +format(minutes) ;
addtime.setText(string_date);
Log.e("JEJEJEJE",string_date);
SimpleDateFormat f = new SimpleDateFormat("yyyy/mm/dd HH:mm");
try {
Date d = f.parse(string_date);
milliseconds = d.getTime();
Log.e("LONGGGGGGG", String.valueOf(milliseconds));
} catch (ParseException e) {
e.printStackTrace();
}
}
}, hours, minutes, true);
timePickerDialog.show();
}
I suggest: First, instead of your variables (fields?) year
, month
, day
, hours
and minutes
just declare
private LocalDate date;
private LocalTime time;
private long milliseconds;
(Keep the milliseconds
variable since you will want to have your result here.)
In onDateSet
assign a value to date
in this way:
date = LocalDate.of(selectedYear, selectedMonth + 1, selectedDate);
This is where we are taking into account, as you said, that the date picker’s selectedMonth
is 0-based, while LocalDate
numbers months the way humans do, from 1.
Now your timePicker
method becomes
private void timePicker(){
// Get Current Time
time = LocalTime.now(ZoneId.systemDefault());
// Launch Time Picker Dialog
TimePickerDialog timePickerDialog = new TimePickerDialog(this,
android.R.style.Theme_Holo_Light_Dialog,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay,int minute) {
time = LocalTime.of(hourOfDay, minute);
milliseconds = date.atTime(time)
.atZone(ZoneId.systemDefault())
.toInstant()
.toEpochMilli();
Log.e("LONGGGGGGG", String.valueOf(milliseconds));
}
},
time.getHour(),
time.getMinute(),
true);
timePickerDialog.show();
}
You were greatly overcomplicating things. I recommend:
SimpleDateFormat
(which, I might add, is notoriously troublesome, so it is good that you can do without it).java.time
, the modern Java date and time API. It is so much nicer to work with than the old-fashioned date and time classes like Calendar
.The two points go hand in hand in the above snippets. Also as far as I can see you have no use for the Calendar
object (c
) in setDateField
, just remove it and all use of it.
The culprit was your SimpleDateFormat
. Try using it like this:
SimpleDateFormat f = new SimpleDateFormat("yyyy/mm/dd HH:mm");
System.out.println(f.parse("2018/04/17 12:45"));
On my computer this prints
Wed Jan 17 12:45:00 CET 2018
It prints a date in January no matter what is in the string. This is because you tried lowercase mm
for month. mm
is for minutes (which you also used correctly), month is uppercase MM
. So your formatter cannot parse a month at all and assigns your date the default month, which is the first month of the year, January. And this date in January was of course also what went into your millesecond value.
java.time
on Android?Yes, you can use java.time
on Android. Using it just requires Java 6.
org.threeten.bp
and subpackages.java.time
.