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.
Related
This question already has answers here:
Do we have a TimeSpan sort of class in Java
(4 answers)
Closed 6 years ago.
I am trying to convert the following code which is in c# to java. And I am facing difficulty in converting it. Please can anyone suggest me a simple way to do it in Java.
DateTime StartDate = new DateTime(PWUpdatedOn.Year, 01, 01);
TimeSpan ts = new TimeSpan(PWUpdatedOn.Ticks - StartDate.Ticks);
//Response.Write(ts.Days+1);
days = ts.Days + 1;
lngN = 0;
PWUpdatedOn.Year = 2016 // current year
As Jigar Joshi answered in an other Question.
Interval from JodaTime will do..
A time interval represents a period of time between two instants. Intervals >are inclusive of the start instant and exclusive of the end. The end instant is always greater than or equal to the start instant.
Intervals have a fixed millisecond duration. This is the difference between the start and end instants.
The duration is represented separately by ReadableDuration. As a result, intervals are not comparable. To compare the length of two intervals, you should compare their durations.
An interval can also be converted to a ReadablePeriod. This represents the difference between the start and end points in terms of fields such as years and days.
Interval is thread-safe and immutable.
I am not sure what Ticks are in C#. But it would be something like:
LocalDateTime startDate = LocalDateTime.of(PWUpdatedOn.getYear(), 1, 1);
Period ts = Period.between(PWUpdatedOn, startDate.toLocalDate());
days = ts.getDays() + 1;
Note that Period.between() requires two LocalDate instances. If PWUpdateOn is a LocalDateTime instance, it needs to be converted with the method toLocalDate().
Some potentially relevant remarks: for zoned datetimes, use ZonedDateTime rather than LocalDateTimel; all time and period objects are immutable in Java.
This question already has answers here:
Any new method to get current time with accuracy in microseconds in Java now?
(3 answers)
Closed 6 years ago.
In java Calendar class while adding milliseconds I am missing the decimal precession. Since method expects integer
For example:
1000/12 = 83.333333
In java
Calendar c = Calendar.getInstance();
c.add(Calendar.MILLISECOND, (integer));
Which takes only 83 and .33333 is missed.
Does anybody know how to handle this scenario in java ?
java.util.Calendar keeps time information at the level of milliseconds,
An instant in time can be represented by a millisecond value that is an offset from the Epoch, January 1, 1970 00:00:00.000 GMT (Gregorian).
java.time.LocalDateTime has a sensitivity in the level of nanosecond.
Time is represented to nanosecond precision. For example, the value
"2nd October 2007 at 13:45.30.123456789" can be stored in a
LocalDateTime.
so if you need microsecond or nanosecond level information, you can not use java.util.Calendar, and you should use java.time.LocalDateTime.
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.
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.
This question already has answers here:
Closed 10 years ago.
I need to get real world date and time in Java. I use:
Date date = new Date();
But I'm not sure that it is not just system time. I don't need to be dependent on PC local date and time.
If it is so, then is there any way to abstract from it? I mean I need correct time and date. If today is the 1st of May, 2012 and user changed (maybe there was a system error) it to the 1st of December 2000, it shouldn't affect business logic. So is there any alternative to achieve this?
Date only represents an instant in time, in milliseconds since the Unix epoch of January 1st 1970 UTC (modulo leap seconds). It has no concept of a time zone in its data. However, if you use the toString method it will always convert that UTC instant to a local date/time using the system time zone. That confuses a lot of users, making them think that Date contains a time zone - it's just an illusion.
Likewise Date doesn't have any concept of a calendar system (Gregorian, Julian etc) or a "format". Basically it's just a long :)