I need to convert a UNIX epoch time to day and time something like:
e.g. 1309778593 to Monday, 11:23:12
Any help?
You can do this with help of following code-
String epochTime = "1309778593";
Date convertedDate = new Date(Long.parseLong(epochTime) * 1000);
System.out.println(convertedDate);
This will print-
Mon Jul 04 13:23:13 CEST 2011
I think this will help!
Pull the date components from this:
new Date(1309778593)
Related
I am working with expiration date of card. I have a API where I will get expiration date in "yyMM" format as "String". Here I am trying to use
SimpleDateFormat with TimeZone.getTimeZone("UTC")
So my code is like
String a= "2011";
SimpleDateFormat formatter = new SimpleDateFormat("yyMM");
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = formatter.parse(a);
System.out.println(date);
Now problem is, when I am passing 2011 the out it gives is Sat Oct 31 17:00:00 PDT 2020
Here you can see I am passing 11 as month but it is converting it to Oct instead of Nov.
Why?
And what other options I can use to convert string with yyMM to Date with Timezone?
You should use the Java 8 YearMonth class.
String a = "2011";
DateTimeFormatter inputFormat = DateTimeFormatter.ofPattern("yyMM");
YearMonth yearMonth = YearMonth.parse(a, inputFormat);
DateTimeFormatter outputFormat = DateTimeFormatter.ofPattern("MMMM yyyy");
System.out.println(yearMonth.format(outputFormat));
Output
November 2020
You parsed it fine, but it's printed in PDT, your local timezone.
Sat Oct 31 17:00:00 PDT 2020
Well, Date doesn't track timezones. The Calendar class does, which is internal to the formatter. But still, default print behavior is current timezone.
If you logically convert this output back to UTC, and it will be November 1 since PDT is UTC-7.
Basically, use java.time classes. See additional information here How can I get the current date and time in UTC or GMT in Java?
When I am executing the following code
SimpleDateFormat sdf=new SimpleDateFormat("dd/MM/YYYY");
Date today=new Date();
String olddate="03/11/2016";
System.out.println(sdf.parse(olddate));
Why the Result is displaying Sun Dec 27 00:00:00 IST 2015.
However I was expecting Wed Nov 03 00:00:00 IST 2016.
Anyone can please explain. And how can I get the desired output.
Date format is wrong. It should be y instead of Y-
dd/MM/yyyy
Y specify Week year
y specify Year
For a more detailed insight view, check the link Oracle Docs and you'll see the meaning of each character we pass there.
i try to convert string to date , but when after the conversion the month is set to jan , but in input the month is other example 'sep' . following is my code.
Date tempDate = new SimpleDateFormat("mm/dd/yyyy").parse("09/12/2014");
System.out.println("Current Date " +tempDate);
output :
Current Date Sun Jan 12 00:09:00 IST 2014
it is MM/dd/yyyy. not mm/dd/yyyy
Reference these formats Java Date Format Docs:
so mm is to reference minutes in hour while MM is for Month.
That is the reason you are getting:
Current Date Sun Jan 12 00:09:00 IST 2014 from ("09/12/2014")
by default month is Jan while it set minutes to '09' due to mm.
I have the following:
Date now = new Date();
Date futureDate = new Date(now.getYear(), now.getMonth(), now.getDay() + 30);
I want to set the future date value to be 30 days in the future, based on the current date (now).
When I debug this, now is the correct date, and the futureDate is:
Sat Jan 05 00:00:00 EST 2013
Today's date, the value of now is: Sat Dec 29 17:31:58 EST 2012.
This doesn't make sense to me?
I'm using util.Date.
Because getDay() returns day of the week, not day of the month.
So your
now.getDay() + 30
becomes Saturday + 30 = 6 + 30 = 36th December = 5th January
A quick fix would be to replace your code with:
now.getDate() + 30
But as others already suggest, java.util.Date is kind of deprecated. And you should use Calendar.add(). So your code would become something like:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, +30);
You should use Calendar and its method Calendar.add
If you want to use Date, you'll see working with adding days is all kinds of deprecated:
http://docs.oracle.com/javase/7/docs/api/java/util/Date.html
Use new Date(now.getTime() + (MILLISECONDS_IN_DAY * 30)) instead. Or if you're not stuck with Date, use Calendar.
Not only is that constructor deprecated, it only accepts valid days (1-31).
try using java.util.Calendar instead.
Date is not supposed to be used for such calculations.
Have a look at JodaTime which is exelent for such things.
I'm attempting to parse the following string into a date object: 9/14/2012 9:50:56 PM
I'm using the following format:
DateFormat formatter = new SimpleDateFormat("MM/dd/yy HH:mm:ss a");
formatter.setTimeZone(TimeZone.getTimeZone("America/New_York"));
But I keep getting the following date: Fri Sep 14 06:50:56 PDT 2012
I seem to be off by 12 hours (after accounting for the time change). However when I parse the following string: 9/14/2012 1:00:00 AM - I get the right date object: Thu Sep 13 22:00:00 PDT 2012
What I am doing wrong?
if your date is in am/pm format, you should use hh, instead of HH for hours. See the reference: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
What happens here is the 9 is treated as 09 hours in 24 hour format, which is 9 am, so your date is correctly pushed back 3 hours to make it 6 am. With the second date 1 am is 01 hours, and the date is correct.