1

I have a date object which is parsed from a string using SimpleDateFormat("HH:mm").

This date has the correct time, but not the correct day (It's in January 1970), so i create a calendar with that date. Than i create a calendar with the current date and set the hours and minutes to the hours and minutes from the previous calendar.

If i now use newCal.getTime() it gives me the correct dates for times between 12:00 and 23:59 but if i for example give 11:00 i get a date with 23:00h which i cannot explain.

Here the full code:

String dateString = "11:00";
//String dateString = "20:00";

SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");

Date date = sdf.parse(dateString,new ParsePosition(0));

Calendar parsedCal = Calendar.getInstance();
parsedCal.setTime(date);

Calendar newCal = Calendar.getInstance();

newCal.set(Calendar.HOUR, parsedCal.get(Calendar.HOUR));
newCal.set(Calendar.MINUTE, parsedCal.get(Calendar.MINUTE));

System.out.println(newCal.getTime());

For 20:00 i get the correct output for 11:00 i get 23:00 as mentioned.

Samuel
  • 18,286
  • 18
  • 52
  • 88
  • have you checked if the date you created is the first on the DST? in that case you might lack of one hour depending on your zone. – Lucas de Oliveira May 05 '11 at 16:40
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/9/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/9/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [java.time](https://docs.oracle.com/javase/9/docs/api/java/time/package-summary.html) classes built into Java 8 & Java 9. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Jan 23 '18 at 04:38

4 Answers4

4

You are using Calendar.HOUR; you should be using Calendar.HOUR_OF_DAY

Dilum Ranatunga
  • 13,254
  • 3
  • 41
  • 52
1

Also,

    String dateString = "11:00";
    //String dateString = "20:00";

    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");

    Date date = sdf.parse(dateString);
    Calendar parsedCal = Calendar.getInstance();
    parsedCal.setTime(date);

    Calendar newCal = Calendar.getInstance();

    newCal.set(Calendar.HOUR, parsedCal.get(Calendar.HOUR));
    newCal.set(Calendar.MINUTE, parsedCal.get(Calendar.MINUTE));
    newCal.set(Calendar.AM_PM, parsedCal.get(Calendar.AM_PM));

    System.out.println(newCal.getTime());
Nishant
  • 54,584
  • 13
  • 112
  • 127
0

tl;dr

ZonedDateTime.of(
    LocalDate.now( ZoneId.of( "Pacific/Auckland" ) ) ,  // Current date in a particular time zone.
    LocalTime.parse( "23:00" ) ,                        // Specify 11 PM.
    ZoneId.of( "Pacific/Auckland" )                     // Specify a time zone as the context for this date and time. Adjustments made automatically if that date-time is not valid in that zone.
)

2018-01-23T23:00+13:00[Pacific/Auckland]

java.time

The modern approach uses java.time classes instead of the troublesome old legacy date-time classes.

java.time.LocalTime

For a time-of-day only, without a date and without a time zone, use the LocalTime class.

LocalTime lt = LocalTime.parse( "23:00" ) ;

To determine a specific point on the timeline with that time-of-day, apply a date (LocalDate) and a time zone (ZoneId) to produce a ZonedDateTime object.

LocalDate

The LocalDate class represents a date-only value without time-of-day and without time zone.

LocalDate ld = LocalDate.of( 2018 , Month.JANUARY , 23 ) ;

A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.

ZoneId

Specify a proper time zone name in the format of continent/region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 3-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!).

ZoneId z = ZoneId.of( "Africa/Tunis" );

Use that zone to get the current date.

LocalDate ld = LocalDate.now( z ) ;

ZonedDateTime

Combine.

ZonedDateTime zdt = ZonedDateTime.of( ld , lt , z ) ;

zdt.toString(): 2018-01-23T23:00+01:00[Africa/Tunis]

Keep in mind that for that particular zone, your date and time-of-day may not be valid. The ZonedDateTime class adjusts automatically. Study the doc to be sure you understand and agree with its algorithm for that adjustment.


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

I'd recommend looking at Joda-time, which has classes representing date, time and date-time. Your code snippet could be replaced by:

String dateString = "11:00";
LocalTime time = new LocalTime(dateString);

System.out.println(time.toDateTimeToday());
Kkkev
  • 4,716
  • 5
  • 27
  • 43
  • FYI, the [Joda-Time](http://www.joda.org/joda-time/) project is now in [maintenance mode](https://en.wikipedia.org/wiki/Maintenance_mode), with the team advising migration to the [java.time](http://docs.oracle.com/javase/9/docs/api/java/time/package-summary.html) classes. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Jan 23 '18 at 04:26