ForceFieldsForDoors
ForceFieldsForDoors

Reputation: 193

Getting time in Time format

I am currently trying to work with Location updating.

I've been using the below to work out the time-stamps of when the last update was processed:

String NewLocationTime = DateFormat.getTimeInstance().format(new Date());

However, to work out the difference between two times, I needed to parse it to a Time format. However, for some reason the Android Studio didn't recognize the DateTimeFormatter, and because I don't know in what format the String is going to come up, I am not quite sure what to put in the formatting either.

I believe it would be much easier if I was to be able to get a Time object straight away, so I can use something like:

long diffInMinutes = java.time.Duration.between(dateTime1, dateTime2).toMinutes(); 

Can anyone tell me how I get the Time object straight away, or why the Android Studio does not recognize the DateTimeFormatter?

Thank you

Upvotes: 0

Views: 42

Answers (2)

totoro
totoro

Reputation: 2456

You need to use Date and use Date.getTime() for difference calculation. I do though recommend Joda-Time.

Also look at Calendar.

Time an sql aware wrapper around Date and there is no such thing as DateTime or DateTimeFormatter.

Upvotes: 1

aborocz
aborocz

Reputation: 199

Use new Date().getTime() and what you get is the time in milliseconds and you can work with it as you want.

Upvotes: 1

Related Questions