scalaz
scalaz

Reputation: 81

Shift date from remote source with local

Received data from remote resource

Upvotes: 0

Views: 64

Answers (2)

Misha
Misha

Reputation: 28133

Your format string doesn't match your sample input. To match the input, your format string should be "yyyy-MM-dd HH:mm:ss VV"

As @JonSkeet and @BasilBourque suggest, you should parse an Instant or a ZonedDateTime from the input string. I don't see the question of parsing an Instant well-addressed on SO, so here's how you do it:

// may want to declare and maintain this elsewhere
DateTimeFormatter remoteFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss VV");

Instant remote = remoteFormat.parse(s, Instant::from); 

boolean remoteInThePast = Instant.now().isAfter(remote);

Upvotes: 2

Basil Bourque
Basil Bourque

Reputation: 338730

a String != a Date-Time

You are conflating date-time values with their formatted String representation. You need to parse the input string into a date-time object. Then generate another date-time object to represent "now". Compare the two objects by calling the isBefore or isAfter method.

Similarly, do not confuse the formatted String $12.34 with a number. How would you compare that to another number? You would parse the input string into a number object, perform your business logic, and lastly generate a String representation of the result for display to the user. Ditto for date-time work.

Do not worry about shifting time zones. Any pair of Instant or ZonedDateTime may be compared via isBefore or isAfter regardless of time zone.

I'll not show any code as this Question is really a duplicate of hundreds of other Questions. Please search StackOverflow to learn much more. Search for words "java date parse" and "java date compare". Focus on Answers involving "java.time" or "Java 8".

Upvotes: 2

Related Questions