Converting Integer time stamp into java date [duplicate] - java

This question already has answers here:
Unix epoch time to Java Date object
(7 answers)
Closed 5 years ago.
I have following time stamp in Integer form
1333125342
I can convert it using SQL:
select DATEADD(ss, FlOOR(1333089223/86400)*86400, '1970-01-01 00:00:00') AS Date
How to convert it in java? So that it would return value:
3/30/12 12:18:43 PM

Assuming its the time since 1/1/1970 in seconds. you can try
String dateAsText = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
.format(new Date(1333125342 * 1000L));

if it is milliseconds value represents the number of milliseconds that have passed since January 1, 1970 00:00:00.000 GMT
then simply use
new java.util.Date(millis);
and if you need it in particular format
3/30/12 12:18:43 PM
then use SimpleDateFormat to format the Date to desired formatted String

That timestamp contains the seconds elapsed since 1970-1-1 0:00 UTC.
To convert it to a Java Date instantiate a new Date Object (see Java doc) and invoke setTime() on that. Note, that setTime expects milliseconds instead of seconds, so you would have to multiply your timestamp by 1000.
The toString() method yields something readable.

Related

Difference between hours (MM/dd/yyyy HH:mm:ss.SSS) [duplicate]

This question already has answers here:
How to find difference between two Joda-Time DateTimes in minutes
(4 answers)
Closed 7 years ago.
I have to implement a java program to make the difference between two hours. The problem is that my "hour pattern" is like this:
MM/dd/yyyy HH:mm:ss.SSS
I have to consider ms, too.
I tried using joda:
String sDateStart = "2015/10/14 22:37:28.648";
String sDateFiish = "2015/10/14 22:39:13.573";
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss.SSS");
Date dt1 = format.parse(sDateFiish);
Date dt2 = format.parse(sDateStart);
DateTime start = new DateTime(dt1);
DateTime finish = new DateTime(dt2);
System.out.println(Days.daysBetween(start, finish));
System.out.println(Minutes.minutesBetween(start, finish));
System.out.println(Seconds.secondsBetween(start, finish));
Is there any method to consider the ms?
If you are using Java API itself. In Date class we have getTime method which gives time in millisecond
getTime()
Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.
System.out.println((long)(dt1.getTime() - dt2.getTime()));
If you wanna continue using Joda Time API use Duration. as mentioned by Sotirios Delimanolis

Converting long time to a java date [duplicate]

This question already has answers here:
Converting Integer time stamp into java date [duplicate]
(3 answers)
Closed 8 years ago.
I get a date from a request with this format > 1413972425000
When I execute in JavaScript
new Date(1413972425000)
the result is
Wed Oct 22 2014 11:07:05 GMT+0100 (GMT Daylight Time)
So.. What I want is get the datetime but in Java and I don't know how can I do it.
Thanks.
1413972425000 isn't a int value. It is too large for int. You can use it as a long value. 1413972425000L
You can use
Date date=new Date(1413972425000L); // accept long value.
System.out.println(date);
You can use new Date(1413972425000L) to convert long to date. Note the appended L to the numeric value.

Convert unix timestamp to date in java [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 date in GMT from unix timestamp [duplicate]

This question already has answers here:
How can I get the current date and time in UTC or GMT in Java?
(33 answers)
Closed 9 years ago.
I have written the following code to get the date in GMT from a unix timestamp
private Date converToDate(String unixTimeStamp)
{
//unix timestamps have GMT time zone.
DateFormat gmtFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
gmtFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
//date obtained here is in IST on my system which needs to be converted into GMT.
Date time = new Date(Long.valueOf(unixTimeStamp) * 1000);
String result = gmtFormat.format(time);
return lineToDate(result, true);
}
this code upon execution has
Mon May 27 02:57:32 IST 2013
value in the date variable and
Sun May 26 21:27:32 GMT 2013
in the result variable , How do I directly get the value in result variable into date variable ?
This is the problem, conceptually:
//date obtained here is in IST on my system which needs to be converted into GMT.
Date time = new Date(Long.valueOf(unixTimeStamp) * 1000);
A Date doesn't have a time zone. This is the value you want. The fact that when you call toString() it converts it to your local time zone is irrelevant to the value that it's actually representing. A Date is just a number of milliseconds since the Unix epoch (1st January 1970, midnight UTC). So your whole method can be:
private static Date convertToDate(String unixTimeStamp)
{
return new Date(Long.valueOf(unixTimeStamp) * 1000);
}
You don't need any kind of formatter, as you're not really trying to get a textual representation.
I would advise you to use Joda Time for date/time work if you can, by the way - it's a much cleaner API.
A Date is just the wrapper for a long, which contains a number of milliseconds.
What you're seeing is the default toString() representation of the Date object, which uses your default timezone (IST) to transform the date into a readable string. If you want the date represented as a string using the GMT timezone, just do what you did: use a date format with the GMT time zone.
The Date object represents an instant on the universal timeline, and doesn't have any timezone.

Android get Current UTC time [duplicate]

This question already has answers here:
How can I get the current date and time in UTC or GMT in Java?
(33 answers)
Closed 9 years ago.
What is the function to get the current UTC time. I have tried with System.getCurrentTime but i get the current date and time of the device.
Thanks
System.currentTimeMillis() does give you the number of milliseconds since January 1, 1970 00:00:00 UTC. The reason you see local times might be because you convert a Date instance to a string before using it. You can use DateFormats to convert Dates to Strings in any timezone:
DateFormat df = DateFormat.getTimeInstance();
df.setTimeZone(TimeZone.getTimeZone("gmt"));
String gmtTime = df.format(new Date());
Also see this related question.

Categories

Resources