Keď porovnávam dva dátumy spolu
Nesprávny počet dní !!!
final String myDate = "08/22/2018 20:56:03";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("mm/dd/yyyy HH:mm:ss");
System.out.println("timeeeeeeeeeeeee" + "-" + simpleDateFormat);
Date date = null;
try {
date = simpleDateFormat.parse(myDate);
} catch (ParseException e) {
e.printStackTrace();
}
long endtime = date.getTime();
Date d = null;
try {
d = simpleDateFormat.parse(simpleDateFormat.format(new Date(System.currentTimeMillis())));
} catch (ParseException e2) {
e2.printStackTrace();
}
Long ToCount = endtime - d.getTime();
CountDownTimer cdt = new CountDownTimer(ToCount, 1000) {
public void onTick(long millisUntilFinished) {
holder.timer.setText(""+ String.format(
" %d : %d : %d : %d ",
TimeUnit.MILLISECONDS.toDays(millisUntilFinished),
TimeUnit.MILLISECONDS.toHours(millisUntilFinished) -
TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(millisUntilFinished)),
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) -
TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millisUntilFinished)),
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))
));
}
public void onFinish() {
// TODO Auto-generated method stub
holder.timer.setText("done");
}
}.start();
Neviem presne, čo je chyba Teraz sa vypočíta suma nesprávneho dňa
odpovede:
0 pre odpoveď č. 1mm
a MM
sú odlišné.
MM
je pre Month
.
mm
je pre minute
.
Takže formát musí byť MM/dd/yyyy HH:mm:ss
.
A neviem, ako ste porovnali dve hodnoty.
Môžeš použiť equal()
metóda. Porovnáva jeho hodnotu. ==
porovnáva svoju adresu.
0 pre odpoveď č. 2
Domnievam sa, že váš problém spočíva v tom, že dávate nesprávny formát dátumu na analýzu v porovnaní s dátumom, ktorý poskytujete, na analýzu dátumu, ako je ten váš. "08/22/2018 20:56:03"
, Verím, že nasledujúci formát by bol správny:
public static final String STORY_DATE_FORMAT = "MM/dd/yyyy HH:mm:ss";
Úplným riešením by teda bolo:
final String myDate = "08/22/2018 20:56:03";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(STORY_DATE_FORMAT, Locale.ENGLISH);
Date currentDate = new Date();
Date updatedTimeParsed = simpleDateFormat.parse(myDate);
Date currentDateParsed = simpleDateFormat.parse(simpleDateFormat.format(currentDate));
// get the differences in dates
long different = currentDateParsed.getTime() - updatedTimeParsed.getTime();