how to calculate time duration with the times are String [duplicate] - java

This question already has answers here:
How can I calculate a time difference in Java?
(19 answers)
Closed 7 years ago.
I really need your help, I am working on a project for an airport. I need to calculate the duration of the longest flight(in minutes). What I do have is Departure time and arrival time which are both in String. I have not done anything yet because I am clueless as to what I must do.

You shall use the class java.text.SimpleDateFormat to parse your string into a java.util.Date object. With the method:
public Date parse(String source) throws ParseException
At the creation of the format, you will specify the pattern your time/date text has. FYI, it will be important to consider the timezone. If your input strings contains it: perfect. If not, be sure to take it into account.
Once you have your Date object, extract its time with the getTime() method. It will return a long value with the milliseconds from 1970 in GMT timezone.
If you get this long value for both your departure and arrival time, the difference will tell you the number of milliseconds of the trip.

Related

Generate Date from System.nanoTime() [duplicate]

This question already has answers here:
How can I convert the result of System.nanoTime to a date in Java?
(3 answers)
Closed 7 years ago.
I am writing a code for implementing Stop Watch. I capture a moment with System.nanoTime(). But I would also like to convert and store that moment into a date field. When I try to use new Date(long msec), it's giving me some absurd date-time value. Can anyone help me how to get this done?
System.nanoTime is not the current time:
This method can only be used to measure elapsed time and is not related to any other notion of system or wall-clock time.
This is why you're experiencing "some absurd date-time value".
Use System.currentTimeMillis if you want the date(s) you've captured as milliseconds (see: unix time):
the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC.

How to guess unknown dateTime format? [duplicate]

This question already has answers here:
How to get date datatype from sql database to java?
(2 answers)
Closed 7 years ago.
I have a DateTime which I received as JSON via a REST Servce of the Couchbase's Sync Gateway:
"2015-05-20T13:32:25.9999478-07:00"
I do not have the access to the Sync Gateway's configs.
I did no find any documentation about the default format of Sync Gateway's dateTime format.
I do not understand what the .9999478-07:00" means.
Is there a way to guess that somehow?
That is the ISO standard notation for date time
YYYY-MM-DD,
then a 'T' for time, HH:MI:SS.S* (fractional seconds),
+/- time zone (there are half our zones!)
Time and also time zone optional.
Look in the wikipedia or javadoc.
When no time zone the date time representation can be sorted alphabetical to be naturally ordered.

Time Zone cannot be changed in Calendar util [duplicate]

This question already has answers here:
Working with various Calendar TimeZone in Java (without using Joda Time)
(3 answers)
Closed 9 years ago.
This sentence is supposed to get time information at Chicago time zone:
Calendar.getInstance(TimeZone.getTimeZone("America/Chicago")).getTime();
My problem is no matter what string I put in getTimeZone(), result would be changed.
Could anyone explain this situation?
The key thing to understand is that a java.util.Date represents UTC only - it has no time zone information. Time zones are presentation layer only - they are used to figure out how to display the time represented by the java.util.Date.
So if you use SimpleDateFormat or the Calendar.get(...) methods, the time zone will be taken into account.

Parse DateTime type in C# sent in JSON [duplicate]

This question already has answers here:
Parsing a JSON date info into a C# DateTime
(4 answers)
Closed 9 years ago.
How do I parse Json date in java {"UserCreationTime":"/Date(1348477516620+0530)/"} this is json response i got from .net wcf service, it is basically DateType type in C#.
Thanks in advance.
The first number, 1348477516620 is the number of milliseconds since 1/1/1970 UTC.
The second number +0530 is the UTC offset of the system that created this value, at this specific point in time. But that number is not reflected in the first value in any way.
In other words, if all you care about is a specific instance in time, throw away the second part and just use the first part.
Date date = new Date(1348477516620);
And yes, it's an ugly format and nobody likes it. It's being slowly phased out in favor of ISO8601.

how to detect day light saving in java [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to get the current date and time of your timezone in Java?
I have developed a Attendance System and in use for India. Our servers are in US and since they are using PDT. My code reflects time one hour ahead.
say its 9:00 am IST ---- I get the time as 10:00 am IST
other than detecting one hour from the time, which will be a temporary solution.
Pls suggest me some way to overcome this situation
To check if a given Date is affected by daylight saving, use
Calendar c = Calendar.getInstance(timezone); // omit timezone for default tz
c.setTime(date); // your date; omit this line for current date
int offset = c.get(Calendar.DST_OFFSET);
0 means no DST, any other value (most likely 3600000) means that this date is affected by DST

Categories

Resources