From timestamp to normal date [duplicate] - java

This question already has answers here:
Java: Date from unix timestamp
(11 answers)
Closed 6 years ago.
I have TimeStamp time as String 1374160160 which is equivalent 07/18/2013.
If there is a way how I can convert this TimeStamp to date with format July 18, 2013 ?
This is my code snippet:
try{
String timeStamp = "1374160160";
DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Date netDate = (new Date(timeStamp));
return sdf.format(netDate);
} catch(Exception ex) {
return "";
}
It always returns 01/17/1970 why?
The second problem is that new Date(String) is deprecated.
To sum up:
How to create normal date (July 18, 2013) from time-stamp and avoid the deprecated methods ?

Your timeStamp instance holds seconds but Java Date constructor expects milliseconds. Via documentation:
Allocates a Date object and initializes it to represent the specified number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT
So you should use this:
String timeStamp = "1374160160";
long timeMillis = Long.valueOf(timeStamp) * 1000;
Date netDate = new Date(timeMillis);
instead of:
String timeStamp = "1374160160";
Date netDate = (new Date(timeStamp));

Related

How do I convert date formatted string timestamp into date? [duplicate]

This question already has answers here:
want current date and time in "dd/MM/yyyy HH:mm:ss.SS" format
(11 answers)
display Java.util.Date in a specific format
(11 answers)
Closed 2 years ago.
I need to convert the current timestamp and current minus 1-minute timestamp into a given format. I was able to convert the desired format. But it was returning as String.
I need the formatted output as Date instead of String.
public static void main(String[] args) throws ParseException
{
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:00");
long now = System.currentTimeMillis();
String toTimeStamp = dateFormatter.format(now);
long nowMinus1Minutes = now - TimeUnit.MINUTES.toMillis(1);
String fromTimeStamp = dateFormatter.format(nowMinus1Minutes);
System.out.println(fromTimeStamp);
System.out.println(toTimeStamp);
// Tried with parse
Date fromDate = dateFormatter.parse(fromTimeStamp);
Date toDate = dateFormatter.parse(toTimeStamp);
System.out.println(fromDate);
System.out.println(toDate);
}
Output:(String)
2020-07-24 12:13:00
2020-07-24 12:12:00
Output: Pose Parsed the string as Date
Fri Jul 24 12:16:00 IST 2020
Fri Jul 24 12:17:00 IST 2020
Expected Output:
2020-07-24 12:13:00 (As Date Object)
2020-07-24 12:12:00 (As Date Object)
Can't be possible. Understood lately. Closing this.
Once you transform your String into a Date, the initial format you used, whatever it is is lost.
If you want to display your data in the "yyyy-MM-dd HH:mm:00", use your formatter again otherwise Date class will stick to its default format:
System.out.println(formatter.format(fromDate));
System.out.println(formatter.format(toDate));
If you need two Date-Object, you could do it like this:
OffsetDateTime offsetDateTime1 = OffsetDateTime.now();
OffsetDateTime offsetDateTime2 = offsetDateTime1.minusMinutes(1);
System.out.println(Date.from(offsetDateTime1.toInstant()));
System.out.println(Date.from(offsetDateTime2.toInstant()));
If you need the timestamp to be printed in other format, you can use SimpleDateFormat on the Date-Objects afterwards.

Convert "2020-10-31T00:00:00Z" String Date to long [duplicate]

This question already has answers here:
Java SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'") gives timezone as IST
(9 answers)
How to validate the DateTime string format "2018-01-22T18:23:00.000Z" in Java?
(2 answers)
parsing date/time to localtimezone
(2 answers)
Closed 3 years ago.
I am having Input Date as "2020-10-31T00:00:00Z". i want to parse this Date to get Long milliseconds.
Note: Converted milliseconds should be in Sydney Time (ie GMT+11).
FYI,
public static long RegoExpiryDateFormatter(String regoExpiryDate)
{
long epoch = 0;
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
df.setTimeZone(TimeZone.getTimeZone("GMT+11"));
Date date;
try {
date = df.parse(regoExpiryDate);
epoch = date.getTime();
} catch (ParseException e) {
System.out.println("Exception is:" + e.getMessage());
e.printStackTrace();
}
System.out.println("Converted regoExpiryDate Timestamp*************** " + epoch);
return epoch;
}
Output: 1604062800000 which gives Date as 30/10/2019 by using Epoch Converter, but in input i'm passing 31st as Date.
Can anyone please clarify this?
By doing df.setTimeZone(TimeZone.getTimeZone("GMT+11"));, you are asking the date formatter to interpret your string in the GMT+11 time zone. However, your string shouldn't be interpreted in that timezone. See that Z in the string? That stands for the GMT time zone, so you should have done this instead:
df.setTimeZone(TimeZone.getTimeZone("GMT"));
In fact, your string is in the ISO 8601 format for an Instant (or a "point in time", if you prefer). Therefore, you could just parse it with Instant.parse, and get the number of milliseconds with toEpochMilli:
System.out.println(Instant.parse("2020-10-31T00:00:00Z").toEpochMilli());
// prints 1604102400000
Warning: you shouldn't really use SimpleDateFormat anymore if the Java 8 APIs (i.e. Instant and such) are available. Even if they are not, you should use NodaTime or something like that.

Applying timezone to date format not working [duplicate]

This question already has answers here:
Converting ISO 8601-compliant String to java.util.Date
(31 answers)
Closed 6 years ago.
In my Android app I have a string like this, String date = "2016-09-24T06:24:01Z";
I use this code to turn it into a nicer looking date format:
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
dateFormat.setTimeZone(TimeZone.getDefault());
DateFormat formatted = new SimpleDateFormat(format);
Date result = dateFormat.parse(date);
dateString = formatted.format(result);
However it's not applying the timezone. I've tried setting it on both dateFormat and formatted and no matter what I do it still comes back with 6:24 AM.
Shouldn't TimeZone.getDefault() be looking at the timezone on the device running the app and adjusting the time accordingly?
As your are using java.util.date which have no Time Zone. It represent UTC/GMT no Time Zone offset. See below thing.
so change this line
dateFormat.setTimeZone(TimeZone.getDefault());
to this
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
or this
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
If you know the current time zone:
TimeZone tzone = TimeZone.getTimeZone("America/Los_Angeles");
tzone.setDefault(tzone);
If you do not know the current timezone:
Calendar cal = Calendar.getInstance();
long milliDiff = cal.get(Calendar.ZONE_OFFSET);
// Got local offset, now loop through available timezone id(s).
String [] ids = TimeZone.getAvailableIDs();
String name = null;
for (String id : ids) {
TimeZone tz = TimeZone.getTimeZone(id);
if (tz.getRawOffset() == milliDiff) {
// Found a match.
name = id;
break;
}
}
TimeZone tzone = TimeZone.getTimeZone(name);
tzone.setDefault(tzone);

java - Convert date string to UTC time [duplicate]

This question already has answers here:
Parsing ISO-8601 DateTime with offset with colon in Java
(4 answers)
Closed 6 years ago.
I just needed a sample code block or suggestion to convert the following date string to utc time and find difference with current time in java?
date string "2016-03-21T15:58:36-04:00"
Thanks in advance
You need to format the date string in following way:
String string = "2016-03-21T15:58:36-04:00";
DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", Locale.ENGLISH);
Date date = format.parse(string);
Then you just need to use Date APIs to find the time difference.
public static long getMillisFrom(String inStrDate) {
DateFormat ISO_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ENGLISH);
Date inDate = ISO_DATE_FORMAT.parse(inStrDate);
Date curDate = new Date();
long diffMillis = curDate().getTime() - inDate.getTime();
return diffMillis
}

Convert epoch time to dd/MM/yyyy using JAVA [duplicate]

This question already has answers here:
Unix epoch time to Java Date object
(7 answers)
Closed 9 years ago.
I need to convert a String which is a epoch (Unix time) format to a Date class an after a String formatted (dd/MM/yyyy).
Thank you for your help !
Unix time is the number of seconds since 1 January 1970, so this should work
Date date = new Date(unixTime * 1000);
String str = new SimpleDateFormat("dd/MM/yyyy").format(date);
BTW SimpleDateFormat accepts millis as argument too, so it is possible to get the same result as
String str = new SimpleDateFormat("dd/MM/yyyy").format(unixTime * 1000);
Date date = new Date(time);
DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
format.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
String formatted = format.format(date);
System.out.println(formatted);

Categories