How to convert current UTC time into Linux Timestamp [duplicate] - java

This question already has answers here:
How can I get the current date and time in UTC or GMT in Java?
(33 answers)
Android Get Current timestamp?
(14 answers)
Closed 6 years ago.
I am trying to get current android device time, and convert it into UTC timezone, then i need to convert it into Unix Timestamp.
I google it, found some solutions, tried few, but nothing helping me here.
This is what i am doing now.
Date date;
SimpleDateFormat dateFormatGmt = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
dateFormatGmt.setTimeZone(TimeZone.getTimeZone("UTC"));
SimpleDateFormat dateFormatLocal = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
date= dateFormatLocal.parse( dateFormatGmt.format(new Date()) );
date.getTime();
Output:
date(Its returning the correct UTC date time) Thu Jan 26 08:06:20 GMT+05:00 2017
date.getTime() returns 1485399980000
When i put this Timestamp in online tools, Its not returning right output.
Kindly guide me how to convert current UTC time into UnixTimestamp

What you need is much simpler:
new Date().getTime()
It is alread in UTC. To get a Linux timestamp you have to divide this by 1000.

Related

How to print UTC or GMT using SimpleDateFormat? [duplicate]

This question already has answers here:
Convert Java Date to UTC String
(7 answers)
How do you format current system datetime as UTC using String.format in Java?
(2 answers)
How to get UTC+0 date in Java 8?
(5 answers)
Closed last month.
This post was edited and submitted for review last month and failed to reopen the post:
Original close reason(s) were not resolved
I am trying to print a date in this format:
2023-01-11 09:25:52 UTC
But when I use date format:
yyyy-MM-dd HH:mm:ss Z
I get:
2023-01-11 09:29:25 +0100
While searching by existing question in stack overflow, I found similar questions but not with this exact format with "UTC" at the end. I found one that provided a solution to add the format as yyyy-MM-dd HH:mm:ss 'UTC' but then it would force the a GMT value to wrongly show UTC at the end.
Most of the answers explain how to get UTC value, but not how to print in this format
2023-01-11 09:25:52 UTC
Some solutions were also suggesting to use something else than SimpleDateFormat.
This question was marked as duplicated, but none of the post that were supposed to be duplicated had the info that I wanted.
Use a lowercase z instead of Z to get the offset instead of the id. And you have to set the time zone using simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC")).
Example:
Date date = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println(simpleDateFormat.format(date)); // 2023-01-11 08:41:17 UTC
If you create an instance of simpleDateFormat with yyyy-MM-dd HH:mm:ss Z date format is incorrect change to capital Z to small z,
then it works as expected.
2023-01-11 09:25:52 UTC

How Covert unknown date format to UTC in Java [duplicate]

This question already has answers here:
how to convert milliseconds to date format in android?
(17 answers)
Closed 3 years ago.
I have to convert the following time to UTC but I don't know what format the current time is in. How do I convert it to UTC using Java?
Unknown format:
1561554154352
It clear to me which or how to do it based on the Java Date documentation.
https://docs.oracle.com/javase/8/docs/api/java/util/Date.html
That isn't so much a Date as it is an offset to an epoch (specifically, the number of milliseconds since midnight January 1, 1970 UTC). To convert it to a date, pass that number to the date constructor. Like,
long epochTime = 1561554154352L;
System.out.println(new Date(epochTime));
Outputs
Wed Jun 26 09:02:34 EDT 2019
The following will get you from Zulu(UTC):
System.out.println("Current elapsed from epoch => " + System.currentTimeMillis());
System.out.println("Convereted to UTC =>" + Instant.ofEpochMilli(System.currentTimeMillis()).toString());

Android timestamp to local user time or date [duplicate]

This question already has answers here:
Unix epoch time to Java Date object
(7 answers)
Closed 5 years ago.
How can I convert minutes from Unix timestamp to date and time in java? For example, timestamp 1372339860 correspond to Thu, 27 Jun 2013 13:31:00 GMT.
I want to convert 1372339860 to 2013-06-27 13:31:00 GMT.
Edit: Actually I want it to be according to US timing GMT-4, so it will be 2013-06-27 09:31:00.
You can use SimlpeDateFormat to format your date like this:
long unixSeconds = 1372339860;
// convert seconds to milliseconds
Date date = new java.util.Date(unixSeconds*1000L);
// the format of your date
SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
// give a timezone reference for formatting (see comment at the bottom)
sdf.setTimeZone(java.util.TimeZone.getTimeZone("GMT-4"));
String formattedDate = sdf.format(date);
System.out.println(formattedDate);
The pattern that SimpleDateFormat takes if very flexible, you can check in the javadocs all the variations you can use to produce different formatting based on the patterns you write given a specific Date. http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Because a Date provides a getTime() method that returns the milliseconds since EPOC, it is required that you give to SimpleDateFormat a timezone to format the date properly acording to your timezone, otherwise it will use the default timezone of the JVM (which if well configured will anyways be right)
Java 8 introduces the Instant.ofEpochSecond utility method for creating an Instant from a Unix timestamp, this can then be converted into a ZonedDateTime and finally formatted, e.g.:
final DateTimeFormatter formatter =
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
final long unixTime = 1372339860;
final String formattedDtm = Instant.ofEpochSecond(unixTime)
.atZone(ZoneId.of("GMT-4"))
.format(formatter);
System.out.println(formattedDtm); // => '2013-06-27 09:31:00'
I thought this might be useful for people who are using Java 8.
You need to convert it to milliseconds by multiplying the timestamp by 1000:
java.util.Date dateTime=new java.util.Date((long)timeStamp*1000);

Getting 24hr time when adding miliseconds to a Date? [duplicate]

This question already has answers here:
How to add one day to a date? [duplicate]
(18 answers)
Closed 5 years ago.
I have the following Java code that takes a date and should add a full day to the date:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
String dateString = "2017-01-30T19:00:00+0000"
Date date = formatter.parse(dateString);
long timeBetweenStartDates = 24 * 60 * 1000;
Long DateWithOneDayAddedInMilis = date.getTime()+timeBetweenStartDates;
Date dateWithOneDayAdded = new Date((DateWithOneDayAddedInMilis));
The value I am getting for dateWithOneDayAdded is:
Mon Jan 30 13:24:00 GMT 2017
What I am looking for here would be:
Tue Jan 31 13:24:00 GMT 2017
How can I ensure that the date is in the format I expect?
Oh, what a wonderful example of where the newer date and time classes are much more programmer-friendly. With these it’s next to impossible to make an error like the one you made (and which is pretty easy to make with the old classes, and in particular, very hard to spot once you have written the code).
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ");
String dateString = "2017-01-30T19:00:00+0000";
OffsetDateTime dateTime = OffsetDateTime.parse(dateString, formatter);
OffsetDateTime dateWithOneDayAdded = dateTime.plusDays(1);
System.out.println(dateWithOneDayAdded);
This prints
2017-01-31T19:00Z
You may of course format the calculated date-time the way you or your users prefer.
An added benefit is that plusDays() handles transistion to and from summer time (DST) nicely and hits the same time on the next day (if possible) rather than blindly adding 24 hours.

get date difference in java, use of getDate() [duplicate]

This question already has answers here:
Calculating the difference between two Java date instances
(45 answers)
getting the difference between date in days in java [duplicate]
(3 answers)
Closed 10 years ago.
I am co-working with a group, I want to ask how can i get the date difference from a formate of "Sat Feb 23 00:00:00 GMT 2013". The pickerfrom and to is a calendar, and getDate returns that formate. How can I get the date difference in days? any idea?
/* Current format Sat Feb 23 00:00:00 GMT 2013 */
Date date_from = pickerFrom.getDate();
Date date_to = pickerTo.getDate();
int date_diff = (int)((date_to)-(date_from));
Checkout getting the difference between date in days in java
My preference would be to use Joda time - it has many useful date functions that'll make your life much easier when it comes to dates and date manipulation
You can get the difference in milliseconds of each date and subtract these values.
long diff = date_to.getTime() - date_from.getTime();
will return you the number of milliseconds between the two dates.
Then, you can use something like this to get the number of hours, days,... out of these milliseconds.

Categories