0

I have the string timeString = "8:30AM" in Java. I don't get the date from the user, only the specific time in the timeString variable.

I need to add the current date to that timeString.

String userTimeString = "8:30AM";   // This is how I get the time from the user

// I need to grab today's date without the hour, and as the hour it should be the userTimeString
// the result should be saved as timeStamp. 

How do I do?

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Guisselle
  • 23
  • 2
  • If you meant the old-fashioned `java.sql.Timestamp`, I recommend you don’t use that class. It’s poorly designed and long outdated. In omst SQL variants use a `timestamp with time zone` and then use a modern `OffsetDateTime` in Java to store into the database. – Ole V.V. Aug 30 '21 at 05:33
  • Welcome to Stack Overflow. Please learn that you’re supposed to search for an answer before posting a new question. Granted, you likely won’t find a place that gives you the full circle from parsing the string over constructing date and time combined to saving to the database. You will find some bits and pieces that will let you ask a more focused question. Showing this effort on your part will also make users here generally willing to do a greater effort on theirs to help you. – Ole V.V. Aug 30 '21 at 05:34
  • There’s (partial) help [here](https://stackoverflow.com/questions/39870160/parse-clock-time-in-java-8) and [here](https://stackoverflow.com/a/67505173/5772882). – Ole V.V. Aug 30 '21 at 05:54
  • The `java.util` Date-Time API and their formatting API, `SimpleDateFormat` are outdated and error-prone. It is recommended to stop using them completely and switch to the [modern Date-Time API](https://www.oracle.com/technical-resources/articles/java/jf14-Date-Time.html). I strongly recommend you use the [solution by Ole V.V.](https://stackoverflow.com/a/68989942/10819573). – Arvind Kumar Avinash Sep 01 '21 at 20:22

1 Answers1

1

java.time

You need to decide on a time zone for that. Once you know your time zone, we can define a couple of constants, for example:

private static final ZoneId ZONE = ZoneId.of("America/Antigua");
private static final DateTimeFormatter TIME_PARSER
        = DateTimeFormatter.ofPattern("h:mma", Locale.ENGLISH);

I am using java.time, the modern Java date and time API. Substitute your time zone instead of America/Antigua. If you want the default time zone of your device, set ZONE to ZoneId.systemDefault().

Now we can do:

    String userTimeString = "8:30AM";
    
    OffsetDateTime timestamp = ZonedDateTime.of(
                    LocalDate.now(ZONE),
                    LocalTime.parse(userTimeString, TIME_PARSER),
                    ZONE)
            .toOffsetDateTime();
    
    System.out.println(timestamp);

Output when I ran today:

2021-08-30T08:30-04:00

Did you want to save the timestamp in your SQL database? Since JDBC 4.2 you can save an OffsetDateTime into an SQL column of data type timestamp with time zone. See the links.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161