Jump to content

[Java] Comparing String timestamps

Shally

Hey guys,

This is probably simple but I can't find an elegant solution.

I have a timestamp like this as a String:

String recvTime =  "2020-08-12T17:30:12.236Z";
String inputTIme =  "2021-09-13T18:50:13.236Z";

Essentially I just want to compare the times to see if recvTime is more recent than inputTime

What is best practice to do such a comparison?  
 

Irish in Vancouver, what's new?

 

Link to comment
Share on other sites

Link to post
Share on other sites

Found it:
 

String recvTime =  "2020-08-12T17:30:12.236Z";
   SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
   System.out.println(sdf.parse(recvTime));

Irish in Vancouver, what's new?

 

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Shally said:

I have a timestamp like this as a String:


String recvTime =  "2020-08-12T17:30:12.236Z";
String inputTIme =  "2021-09-13T18:50:13.236Z";

Essentially I just want to compare the times to see if recvTime is more recent than inputTime

What is best practice to do such a comparison?  

Convert these to a LocalDateTime and use the .isBefore() method to compare the two.

https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Shally said:

I think it's just .before() but thanks! 

I linked the official Oracle documents, which says it's isBefore().

LocalDate a = LocalDate.of(2012, 6, 30);
LocalDate b = LocalDate.of(2012, 7, 1);
a.isBefore(b) == true
a.isBefore(a) == false
b.isBefore(a) == false

 

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

Since both your dates are strings in the yyyy-mm-dd-hh-mm-ss format, you can just compare the strings lexicographically. recvTime > inputTime. If one date's string would appear later than the other in a dictionary, then that date is later.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×