I have a number representing the number of nanoseconds since 12:00 a.m., January 1, 1904, universal time. I wish to instantiate a java.util.Date object representing that date. How should I proceed?
You first need to convert your number representing nanoseconds to milliseconds.
Then for the given date string, get the total number of milliseconds since the unix time Epoch, and then add the number earlier converted to milliseconds to it.
Here's the working code:
String target = "1904/01/01 12:00 AM"; // Your given date string
long nanoseconds = ...; // nanoseconds since target time that you want to convert to java.util.Date
long millis = TimeUnit.MILLISECONDS.convert(nanoseconds, TimeUnit.NANOSECONDS);
DateFormat formatter = new SimpleDateFormat("yyyy/MM/dd hh:mm aaa");
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = formatter.parse(target);
long newTimeInmillis = date.getTime() + millis;
Date date2 = new Date(newTimeInmillis);
System.out.println(date2);
Add an import java.util.concurrent.TimeUnit;.
I think it is trivial:
final GregorianCalendar startDate = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
startDate.clear();
startDate.set(1904, Calendar.JANUARY, 1);
final long startMillis = startDate.getTimeInMillis();
new Date(nanos / 1000 / 1000 + startMillis)
Date date = new Date(new Date().getTime()-(time in nanoseconds/(1000*1000)));
what's wrong in using this? I tested with a value of "time in nanoseconds" for June 8th 1926 and it works. and date format has nothing to do with it, the underlying milliseconds value representing the time is what is required.
First of all java.util.Date
Allocates a Date object and initializes it so that it represents the time at which it was allocated, measured to the nearest millisecond.
So, if you have variable milliseconds
Somehow calculate number of milliseconds between your date and Jan 1, 1970 (Unix epoch) diff
Use Date(long) constructor
new Date(milliseconds - diff);
Related
This question already has answers here:
Android:Display time after adding GMT time zone
(2 answers)
Closed 4 years ago.
I am converting milliseconds to the time of the respective country time format, for example, pakistan, US etc
For example
timeinmilliseconds=1549362600000
So its respective Time formate from which I got these milliseconds is 15:30 or 3:30 in 12 hr format
When I want to convert these milliseconds back to that time
I get 10:30 (Five hrs back)
public String getTimeFromLong(long timeInMilliseconds){
String mytime="";
long minute = (timeInMilliseconds / (1000 * 60)) % 60;
long hour = (timeInMilliseconds / (1000 * 60 * 60)) % 24;
mytime = String.format("%02d:%02d", hour, minute);
return mytime;
}
If I select time 4:00
I converted to that to milliseconds (This part is OK)
And wants the time back from milliseconds but get five hours back
For example, If I select time 9:30
convert it to milliseconds and then to time
I get 4:30
You need to use your local time zone to get the time in your region, the default is being apllied which is the Greenwich Mean Time (GMT). For Pakistan use Asia/Karachi like so:
SimpleDateFormat simpleDateFormat= new SimpleDateFormat("hh:mm");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Karachi"));
Use this method to convert milliseconds into your local time
public String getTime(long time){
Calendar calendar = new GregorianCalendar();
calendar.setTimeInMillis(time);
SimpleDateFormat format = new SimpleDateFormat("hh:mm a");
Date date = new Date(time);
String kTime = format.format(date);
return kTime;
}
Using Java 8 we can do the following.
LocalDateTime dateTime =
LocalDateTime.ofInstant(Instant.ofEpochMilli(longValue), ZoneId.systemDefault());
to get date and time
Use below code to get time from long values:
public String getTimeFromLong(long timeInMilliseconds){
// Creating date format
DateFormat simple = new SimpleDateFormat("dd MMM yyyy HH:mm:ss:SSS Z");
Date result = new Date(timeInMilliseconds);
return simple.format(result);
}
I'm trying to create a java.util.Date object without the milliseconds part. eg: 2018-03-19T15:04:23+00:00. This is the code I have:
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
Date d = new Date();
String strFromD = sf.format(d);
Date dFromStr = sf.parse(strFromD);
When I debug this and inspect the variables, I see this:
The String which I get by formatting the date does not have any milliseconds. However, when I create a date back from the String, it has the milliseconds part.
I tried the same using Joda DateTime as well:
DateTimeFormatter dateFormatter = ISODateTimeFormat.dateTimeNoMillis();
DateTime dt = new DateTime();
String strFromDt = dateFormatter.print(dt);
DateTime dtFromStr = dateFormatter.parseDateTime(strFromDt);
System.out.println("DT : "+dt);
System.out.println("String from DT : "+strFromDt);
System.out.println("DT from String : "+dtFromStr);
And this is the output:
DT : 2018-03-22T09:30:22.996-07:00
String from DT : 2018-03-22T09:30:22-07:00
DT from String : 2018-03-22T09:30:22.000-07:00
Again, when I try to get the DateTime from the String, it adds the milliseconds back.
Am I missing something here? Do I need to use 2 different formatters or something?
If your SDK expects a java.util.Date, there's no point talking about a format, because dates don't have a format.
The Date class represents one numerical value: the number of milliseconds since Unix Epoch (Jan 1st 1970, at midnight, in UTC). To make a date without the milliseconds, you could truncate this milliseconds value:
Date d = new Date();
// truncate the number of milliseconds since epoch (eliminate milliseconds precision)
long secs = d.getTime() / 1000;
// create new Date with truncated value
d = new Date(secs * 1000);
In Joda-Time, it's a little bit simpler:
// set milliseconds to zero
DateTime dt = new DateTime().withMillisOfSecond(0);
// convert to java.util.Date
Date date = dt.toDate();
If your SDK expects a String, though, then it makes sense talking about formats. A date can be represented (aka "transformed in text") in many different ways:
2018-03-22T09:30:22-07:00
March 22nd 2018, 9:30:22 AM
22/03/2018 09:30:22.000
and so on...
Objects like java.util.Date and Joda's DateTime don't have a format. They just hold values (usually, numerical values), so if your SDK expects one of those objects, just pass them and don't worry about it.
If the SDK expects a String in a specific format (a text representing a date), then you should transform your date objects to that format.
And if this format doesn't allow milliseconds, so be it:
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
String dateFormattedAsString = sdf.format(d);
With Joda-Time:
DateTime dt = new DateTime();
DateTimeFormatter fmt = ISODateTimeFormat.dateTimeNoMillis();
String dateFormattedAsString = fmt.print(dt);
Those will not change the date's values, but the strings won't have the milliseconds printed.
What I'm trying to do: Check for the minutes until an event. (I'm in central time which is UTC -5 hours).
The object I get is a JSON Element that looks like this when I take the string:
/Date(1502964420000-0500)/
I should be able to:
//take the departure time and subtract it from the current time. Divide by 60
timeStamp = timeStamp.substring(6,16);
This gives me 1502964420 which I can use a time converter to get: Thursday, August 17, 2017 5:07:00 AM
Problem is.. How do I get the current time in the same format to subtract it?
(or if there's a better way to do this I'd gladly take that advice as well).
I would recommend looking at the datatype ZonedDateTime.
With this you can easily perform calculasions and conversions like this:
ZonedDateTime startTime = ZonedDateTime.now();
Instant timestamp = startTime.toInstant(); // You can also convert to timestamp
ZonedDateTime endTime = startTime.plusSeconds(30);
Duration duration = Duration.between(startTime, endTime);
if(duration.isNegative()){
// The end is before the start
}
long secondsBetween = duration.toMillis(); // duration between to seconds
Since you don't know about ZonedDateTime here is a quick overview how to convert string to ZonedDateTime:
Note: The String has to be in the ISO8601 format!
String example = "2017-08-17T09:14+02:00";
OffsetDateTime offset = OffsetDateTime.parse(example);
ZonedDateTime result = offset.atZoneSameInstant( ZoneId.systemDefault() );
You can either use Date currentDate = new Date() and then currentDate.getTime() to get the current Unix time in milliseconds or use the Calendar-class: Calendar currentDate = Calendar.getInstance() and currentDate.getTime().getTime() to get the current Unix time in milliseconds.
You can do the same with the date parsed from the json and then calculate the difference between the two values. To get the difference in minutes, just divide it then by (60*1000)
I want to subtract two dates (one constant and one current) in Java but I've got strange problems with it. Here is the code :
DateFormat df = new SimpleDateFormat("HH:MM");
Date FirstLessonInterval=df.parse("08:45");
Date currentTime = new Date();
long diff = FirstLessonInterval.getTime()-currentTime.getTime();
String s = String.valueOf(diff);
LessonOrBreak=(diff);
I've got minus minutes. When I want to see FirstLessonInterval with FirstLessonInterval.toString() it shows the year 1970. What can I do?
You forgot to give a date, you just defined a time:
DateFormat df = new SimpleDateFormat("HH:MM");
Date FirstLessonInterval=df.parse("08:45");
and this is in unix time day 0 which is the 1.1.1970
try something like
DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:MM");
Date FirstLessonInterval=df.parse("2014/05/10 08:45");
1970 is where all time began according to computers. Are we missing some code in your question? You can faff around with the current time in milliseconds but i'd take a look at JodaTime and use that.
The reason you are getting 1970.... is because I suspect your diff is quite a small number. Then if you look at that as a date then it will be a small number + 1 Jan 1970 which will still be in 1970. But as i said I suspect we are missing some code in your question.
In JodaTime you can do somethign like the below but im not sure what it is you are exactly after
Interval i= new Interval(new DateTime(FirstLessonInterval), new DateTime());
System.out.println("Interval is: " + i.toDurationMillis());
Your format pattern is incorrect, use lower case mm to represent minutes
When you do not specify much details to the outdated Java date api, it considers the time since UNIX epoch (1st Jan 1970)
Since you are assuming the date to be the same as the constant time parameters you provide and independent of the timezones, you can bring your current date comparable to the time since UNIX epoch:
Staying close to your original code;
DateFormat df = new SimpleDateFormat("HH:mm");
Date firstLessonInterval = df.parse("08:45");
Date currentTime = new Date();
// Format the current date comparable to UNIX epoch (only hold time params)
String dateStr = df.format(currentTime.getTime());
// Parse the modified date string to a date object
Date comDate = df.parse(dateStr);
// Take the difference in millis
long diff = firstLessonInterval.getTime() - comDate.getTime();
String s = String.valueOf(diff);
// Print the number of minutes passed since
System.out.println("Minutes {elapsed since/time to} 08:45 - " + Math.abs(diff) / 1000 / 60);
Missing Date Portion
As the other correct answers said, you are using the java.util.Date class which is a date-time class holding both a date portion and a time portion.
LocalTime
If you truly care about only time of day, with no date and no time zone, then use the LocalTime class found in both the Joda-Time library and the new java.tome package in Java 8. By the way the old java.util.Date and .Calendar classes are notoriously troublesome and should be avoided.
Joda-Time
Here is some code with date-time and time zone.
Using the Joda-Time 2.3 library…
DateTimeZone timeZone = DateTimeZone.forID( "Europe/Warsaw" );
DateTime dateTime = new DateTime( 2014, 1, 2, 8, 45, 0, timeZone );
DateTime now = new DateTime( 2014, 1, 2, 8, 30, 0, timeZone ); // Or DateTime.now( timeZone )
Duration duration = new Duration( dateTime, now ); // or use Period or Interval.
Joda-Time offers intelligent classes and methods of working with a span of time (a Period, Interval, or Duration). For example look at the Minutes class. But if all you need is millseconds, here you go.
long millis = duration.getMillis();
The problem is that you are not providing enough info to SimpleDateFormat. It sets the hour and minutes correctly but nothing else.
DateFormat df = new SimpleDateFormat("HH:mm");
System.out.println(df.parse("08:45")); // Thu Jan 01 08:45:00 GMT 1970
System.out.println(new Date()); // Sun May 11 07:52:50 GMT 2014
If you want your date to be with respect to the current date try this:
Date curr = new Date();
Date date = new Date(curr.getYear(),
curr.getMonth(),
curr.getDate(),
8, 45, 0);
System.out.println(date); // Sun May 11 08:45:00 GMT 2014
System.out.println(curr); // Sun May 11 07:52:50 GMT 2014
long diff = date.getTime() - curr.getTime();
System.out.println("Minutes: " + diff/6000); // Minutes: 53
I dont know if this way is efficient or not but it's an idea anyway:
Date curr = new Date();
Date date = new Date(114, /*114 is 2014 , don't know why*/
6,
16,
8, 45, 0);
System.out.println(curr);
System.out.println(date);
Date x = new Date(curr.getYear() - date.getYear() ,
curr.getMonth() - date.getMonth(),
curr.getDate() - date.getDate(),
curr.getHours() - date.getHours(),
curr.getMinutes() - date.getMinutes(),
curr.getSeconds() - date.getSeconds() );
String startDateString = "2017-03-08";
String finishDateString = "2017-03-10";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd", Locale.ENGLISH);
LocalDate startDate = LocalDate.parse(startDateString, formatter);
LocalDate finishDate = LocalDate.parse(finishDateString, formatter);
Integer day = finishDate.compareTo(startDate);
Integer day will be 3. It means that the difference between two dates equals 3 days
How can I convert the difference of the current time a given time to create a string with the time format: HH:mm ? ex. 18:36
I did the following but, it is not 24Hour format, it will add AM/PM to the end, and it is 3 hours off.
java.util.Date today = new java.util.Date();
java.sql.Timestamp ts1 = new java.sql.Timestamp(today.getTime());
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss");
java.util.Date parsedDate = dateFormat.parse(time);
java.sql.Timestamp ts2 = new java.sql.Timestamp(parsedDate.getTime());
long nowTime = ts1.getTime();
long givenTime = ts2.getTime();
long timeDiff = givenTime - nowTime;
//convert to string
java.util.Date d = new java.util.Date(timeDiff);
result = DateFormat.getTimeInstance(DateFormat.SHORT).format(d);
//Outputs: 6:56 PM for example
Once easy thing you can do is call getTime() for both dates and then subtract them like so:
long timeDiff = today.getTime() - ts1.getTime()
That should give you the difference in miliseconds between the two times. After that you know that one second is 1k miliseconds, 1min i 60s, 1h is 60 minutes and so on.
Take a look at Commons Lang DurationFormatUtils.
Or Joda-Time's PeriodFormatter.