1

Hello I'm trying to convert a string in the format "17:50" to a date in android but when I try to run this code I get the correct hour from the string but the full date is from 1970. I need this date to schedule some local notifications on a given time of the day or in the next day.

String dtStart = "17:50";
SimpleDateFormat format = new SimpleDateFormat("H:mm");
try {
    Calendar cal = Calendar.getInstance();
    Date date = format.parse(dtStart);
    cal.setTime(date);
    System.out.println(cal.getTime());
} catch (ParseException e) {
    e.printStackTrace();
}


Thu Jan 01 17:50:00 BRT 1970
Anton Shkurenko
  • 4,301
  • 5
  • 29
  • 64
Ramon Vasconcelos
  • 1,466
  • 1
  • 21
  • 28

3 Answers3

0

It's not an error, your code works well. Just if you want to get current date, you have to add the difference between current day and 1st of January 1970.

Your parsed date gives you 17:30 hours, which means 17 * 60 * 60 * 1000 ms + 30 * 60 + 1000 ms.

This way you can find current day: https://stackoverflow.com/a/1908419/4142087

Community
  • 1
  • 1
Anton Shkurenko
  • 4,301
  • 5
  • 29
  • 64
0

What Anton suggested was correct, and the current day / next day logic is your custom implementation. You have to check current time and if it past that time, jump to setting up the alarm the next day.

mobdev999
  • 27
  • 5
0

java.time

You need a time-of-day class to represent your intended meaning. The legacy date-time classes from the earliest versions of Java lack such a class. The java.sql.Time class pretends to do this, but actually contains a date as well due to poor design decisions.

LocalTime

You want the LocalTime class for a time-of-day value without a date and without a time zone.

It uses a generic 24-hour single-day clock. Adding/subtracting spans of time wraps around the clock since it lacks any concept of dates.

Define a formatting pattern to match your input string.

DateTimeFormatter f = DateTimeFormatter.ofPattern( "H:mm" ) ;  // Uppercase `H` means 24-hour clock, lowercase `h` means 12-hour clock.

Parse input string.

String input = "7:50" ;
LocalTime lt = LocalTime.parse( input , f ) ;

Generate a string in standard ISO 8601 format.

String output = lt.toString() ;

07:50

Perhaps your business logic requires assigning the time-of-day to a date. To determine a moment, a point on the timeline, you must also specify a time zone.

LocalDate ld = LocalDate.of( 2018 , Month.MARCH , 27 ) ;
ZoneId z = ZoneId.of( "Pacific/Auckland" ) ;
ZonedDateTime zdt = ZonedDateTime.of( ld , lt , z ) ;

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.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

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