3

I have a String object which stores the current system time in HH:mm format. I have to convert this to a DATE object and maintain the same HH:mm format. How can this be done.

I tried using Date parse options but everytime I get the response in complete dd-MM-yyyy HH:mm:ss format and not in the required HH:mm format needed.

SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
String getCurrentTime = sdf.format(Calendar.getInstance().getTime());
Date date = sdf1.parse(getCurrentTime);

Expected output should be a Date result in HH:mm format.

Sourav Mehra
  • 435
  • 2
  • 7
  • 23
  • 2
    Dates are not made to do this. Date is an object, the properties of an object won't just go away if you don't fill them. – David3103 Apr 09 '19 at 07:34
  • @David3103 Yes you are right, I know the same but in a specific case we have this requirement since the field in DB where we are trying to store is a Date field and not varchar. So is it possible to achieve this somehow? Or are their any alternatives? – Sourav Mehra Apr 09 '19 at 07:36
  • 3
    If you just want to store hours and minutes in an object, you should make a new object by yourself. Date object is made to store both date and time in that exact format. If you must use the date object for some reason, just fill the unused "fields" with some data and use only the "fields" you need. – Luca Corsini Apr 09 '19 at 07:36
  • 4
    see https://docs.oracle.com/javase/8/docs/api/java/time/LocalTime.html – Scary Wombat Apr 09 '19 at 07:38
  • 4
    Sounds like you database type is wrong, and you should be using a Time field if you just want to store the time part. Also, look into the java.sql.Date and java.sql.Time classes. – TheWhiteRabbit Apr 09 '19 at 07:39
  • 2
    I **strongly** advise you to [drop](https://stackoverflow.com/questions/1969442/whats-wrong-with-java-date-time-api) the `Date`, `Calendar` and `SimpleDateFormat` classes and start using the [new Java Date and Time API](https://www.baeldung.com/java-8-date-time-intro) (in the pacakge `java.time`). – MC Emperor Apr 09 '19 at 07:42

4 Answers4

2

Like mentioned in the comments, Date is an object. Other way around with Date/Time API:

LocalTime time = LocalTime.now(); //current time 
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss"); //set format  
System.out.println(time.format(formatter));
1

You are expecting the output of parse to be a Date object in format HH:mm but this will never be the case because it is eventually a Date object. It will have information of date,month,year, time etc i.e. it is made to store both date and time information.

You get the information HH:mm using the format method which you did and it does give you that information

If you want only information of Hour and minutes, I suggest you to create a new class and populate it's value based on output of format method

Ashishkumar Singh
  • 3,580
  • 1
  • 23
  • 41
1

Instead of using Date use java.time.LocalTime. Example:

LocalTime localTime = LocalTime.now();

This class does not store and represent a date or time-zone. You can use the LocalTime.now() and LocalTime.of() methods to create the current time and specific time object respectively.

You can use the getHour(), getMinute() and getSecond() methods of the LocalTime class to get hour, minute and second respectively.

You can use the plus and minus methods of the LocalTime class to add or subtract hours, minutes etc.

You can use the compareTo(), isAfter() and isBefore() methods of the LocalTime class to compare the LocalTime objects.

BSeitkazin
  • 2,889
  • 25
  • 40
1

Here is the simple example to solve this using java8 Date and Time API:

    DateTimeFormatter parser1 = DateTimeFormatter.ofPattern("HH:mm:ss");
    LocalDateTime ldd1 = LocalDateTime.now();
    System.out.println("DateTimeFormatter ldd1 = " + parser1.format(ldd1));
KayV
  • 12,987
  • 11
  • 98
  • 148