Pergunta

I have 2 DateTime objects. The first i set to midnight just gone, the second i want to set to 12 hours later (noon today).

Both objects are set to midnight just gone. Why is this? I've specified 12 hrs later as a long eg 43300000.

thanks in advance

DateTimeFormatter fmt = DateTimeFormat.forPattern("d-MMM-Y H:mm");
        DateTime lastNightMidnight = new DateTime().toDateMidnight().toDateTime();
        String formattedLastNightMidnight = fmt.print(lastNightMidnight);
        textViewAwayFrom.setText(formattedLastNightMidnight);

        DateTime todayNoon = new DateTime().toDateMidnight().plus(43300000L).toDateTime();
        String formattedTodayNoon = fmt.print(todayNoon);
        textViewAwayTo.setText(formattedTodayNoon);

        Log.e(TAG, "lastNightMidnight = " + lastNightMidnight + " todayNoon = " + todayNoon);
Foi útil?

Solução

  1. You should not use DateMidnight because it is deprecated.

  2. You should also not use plusHours(12) because you don't want to do arithmetic but just set the hour of day.

Best way is (see javadoc):

DateTime dt;
DateTime noon = dt.withHourOfDay(12);

Outras dicas

DateTime todayNoon = new DateTime().toDateMidnight().toDateTime().plusHours(12);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top