Unix timestamp Relative
Top answer
1 of 3
3

Don't use the ancient java.util.Date object. Use the new Java 8 Time API, i.e. parse the 2019-07-03 12:14:11 date string to a LocalDateTime object, then call toLocalDate() to get the date-only LocalDate value, which would then be the key in your map (Map<LocalDate, List<Integer>>).

Your code would then be:

map.computeIfAbsent(invoiceHeader.getInvoice_Date().toLocalDate(), x -> new ArrayList<>())
   .add(invoiceId);

If getInvoice_Date() returns a java.util.Date, then you can get the LocalDate value like this:

Instant invoiceInstant = invoiceHeader.getInvoice_Date().toInstant();
LocalDateTime invoiceDateTime = LocalDateTime.ofInstant(invoiceInstant, ZoneId.systemDefault());
LocalDate invoiceDate = invoiceDateTime.toLocalDate();

Combining the code above gives you:

map.computeIfAbsent(LocalDateTime.ofInstant(invoiceHeader.getInvoice_Date().toInstant(),
                                            ZoneId.systemDefault()).toLocalDate(),
                    x -> new ArrayList<>())
   .add(invoiceId);
2 of 3
2

Here you add/get date + time as key in the map. It will not work because you have to add the date without time if you want to be able to have the same key for same date but with different time.
Note that you should use LocalDateTime instead of Date that is a better designed API and that has the advantage to be immutable, which is advised for map keys.

With a few change you could do that :

LocalDate date = invoiceHeader.getInvoice_Date().toLocalDate();
if(map.containsKey(date)){
    map.get(date).add(invoiceId);        
}
else{    
    List<Integer> invoicesList = new ArrayList<>();
    invoicesList.add(invoiceId);
    map.put(date,invoicesList);
 }

With Map.computeIfAbsent() that provides a fluent API : you could simplify the whole logic such as :

LocalDate date = invoiceHeader.getInvoice_Date().toLocalDate();
map.computeIfAbsent(date, k-> new ArrayList<>()).add(invoiceId);
🌐
Mkyong
mkyong.com › home › java › how to get current timestamps in java
How to Get Current Timestamps in Java - Mkyong.com
March 7, 2025 - import java.sql.Timestamp; import ... ... Starting with Java 8, the modern and preferred way to get a timestamp is by using the Instant class:...
🌐
w3resource
w3resource.com › java-exercises › datetime › java-datetime-exercise-35.php
Java - Extract date, time from the date string
Write a Java program to convert a date-time string into two separate variables for date and time and print them. ... PREV : Today at Midnight. NEXT : Unix Timestamp to Date.
🌐
Baeldung
baeldung.com › home › java › java dates › get the current date and time in java
Get the Current Date and Time in Java | Baeldung
January 8, 2024 - This is useful in scenarios where only the time component is needed, such as scheduling events. The java.sql.Timestamp class combines both date and time, providing nanosecond precision.
Top answer
1 of 5
5

Use java.text.SimpleDateFormat and java.util.TimeZone

Which timezone the date string is in? Replace the below UTC timezone with that timezone

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = sdf.parse("2014-02-15 05:18:08");

SimpleDateFormat sdf2 = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss a");
sdf2.setTimeZone(TimeZone.getTimeZone("IST"));
String dateStr = sdf2.format(date); // Output: 15-02-2014 10:48:08 AM

Note: In which format the hour is in (24 hour/ 12 hour) in your input string? The above example assumes that it is in 24 hour format because there in no AM/PM info in the input string.

If the input string is also in 12 hour format then your input string should mention AM/PM info also such as 2014-02-15 05:18:08 PM. In that case, modify the sdf to new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a")

======================== Edited: =====================

To answer your next question in comment "How to extract date and time separately"...

SimpleDateFormat sdfDate = new SimpleDateFormat("dd-MM-yyyy");
sdfDate.setTimeZone(java.util.TimeZone.getTimeZone("IST"));

SimpleDateFormat sdfTime = new SimpleDateFormat("hh:mm:ss a");
sdfTime.setTimeZone(java.util.TimeZone.getTimeZone("IST"));

String dateStr = sdfDate.format(date);
String timeStr = sdfTime.format(date);
2 of 5
1

The accepted answer by Yatendra Goel is correct.

Joda-Time

For fun, here's the same kind of code using the Joda-Time 2.3 library.

Note that Joda-Time is now in maintenance mode. The team advises migration to java.time. See my other Answer for java.time code.

FYI… India is five and a half hours ahead of UTC/GMT. Hence the thirty minute difference in the outputs below.

String input = "2014-02-15 05:18:08";
input = input.replace( " ", "T" ); // Replace space in middle with a "T" to get ISO 8601 format.

// Parse input as DateTime in UTC/GMT.
DateTime dateTimeUtc = new DateTime( input, DateTimeZone.UTC );
// Adjust to India time.
DateTimeZone timeZone = DateTimeZone.forID( "Asia/Kolkata" );
DateTime dateTime = dateTimeUtc.withZone( timeZone );

// Using "en" for English here because (a) it is irrelevant in our case, and (b) I don't know any Indian language codes.
java.util.Locale localeIndiaEnglish = new Locale( "en", "IN" ); // ( language code, country code );
DateTimeFormatter formatter = DateTimeFormat.forStyle( "SS" ).withLocale( localeIndiaEnglish ).withZone( timeZone );
String output = formatter.print( dateTime );

DateTimeFormatter formatterDateOnly = DateTimeFormat.forPattern( "dd-MM-yyyy" ).withLocale( localeIndiaEnglish ).withZone( timeZone );
DateTimeFormatter formatterTimeOnly = DateTimeFormat.forPattern( "hh:mm:ss a" ).withLocale( localeIndiaEnglish ).withZone( timeZone );
String dateOnly = formatterDateOnly.print( dateTime );
String timeOnly = formatterTimeOnly.print( dateTime );

Dump to console…

System.out.println( "input: " + input );
System.out.println( "dateTimeUtc: " + dateTimeUtc );
System.out.println( "dateTime: " + dateTime );
System.out.println( "output: " + output );
System.out.println( "dateOnly: " + dateOnly );
System.out.println( "timeOnly: " + timeOnly );

When run…

input: 2014-02-15T05:18:08
dateTimeUtc: 2014-02-15T05:18:08.000Z
dateTime: 2014-02-15T10:48:08.000+05:30
output: 15/2/14 10:48 AM
dateOnly: 15-02-2014
timeOnly: 10:48:08 AM
🌐
Java67
java67.com › 2016 › 09 › how-to-get-current-timestamp-value-in-java.html
How to get current TimeStamp value in Java? Example | Java67
Anyway, here is our code prior to Java 8 world: Date now = new java.util.Date(); Timestamp current = new java.sql.Timestamp(now.getTime()); System.out.println("current timestamp: " + current); System.out.println("current date: " + now); Output ...
🌐
SWTestAcademy
swtestacademy.com › home › datetime api in java 8 – timestamp and time operations
DateTime API in JAVA 8 - TimeStamp and Time Operations
February 11, 2019 - Instant instant = Instant.now(); long timeStampMillis = instant.getEpochSecond(); System.out.println(timeStampMillis); ... You can use LocalDate object to get the current system date.
Find elsewhere
🌐
InfluxData
influxdata.com › home › converting timestamp to date in java with examples
Converting Timestamp to Date in Java With Examples
May 7, 2024 - After that, we use this value to create a Date object. If you run the above code, you should get a new timestamp converted to the current date, as shown below: For a more direct approach, you can also use java.util.Date directly.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › sql › Timestamp.html
Timestamp (Java Platform SE 8 )
1 week ago - A thin wrapper around java.util.Date that allows the JDBC API to identify this as an SQL TIMESTAMP value. It adds the ability to hold the SQL TIMESTAMP fractional seconds value, by allowing the specification of fractional seconds to a precision of nanoseconds.
🌐
Baeldung
baeldung.com › home › java › java dates › get date without time in java
Get Date Without Time in Java | Baeldung
March 12, 2025 - Before Java 8, there wasn’t a direct way to get a Date without time unless we were using third party libraries like Joda-time.
🌐
Java67
java67.com › 2016 › 11 › how-to-convert-timestamp-to-date-in-java-jdbc-example.html
How to convert Timestamp to Date in Java?JDBC Example Tutorial | Java67
How to find difference between two dates in Java 8... How to get current Day, Month, Year from Date in J... How to convert Timestamp to Date in Java?JDBC Exam...
🌐
Studytonight
studytonight.com › java-type-conversion › how-to-convert-java-timestamp-to-date
How to convert Java TimeStamp to Date - Studytonight
December 1, 2020 - It must be noted that Date class takes an argument as a long value hence the TimeStamp object needs to be converted into long. This is done using the getTime() method of Timestamp class of the java.sql package. Here, the TimeStamp is converted ...