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.
Related
This question already has answers here:
How can I convert a date in YYYYMMDDHHMMSS format to epoch or unix time?
(2 answers)
Changing String date format
(4 answers)
Change date format in a Java string
(22 answers)
Closed 3 years ago.
I am currently facing a problem when it comes to some Java methods that weren't explained to me in lectures very well. I need to write a program that accepts user-inputted strings (particularly a date) in yyyymmddhhss format, which should then convert to hh:mm Month day, year.
E.g. 201901151500 outputs: "03:00 PM January 15, 2019".
Currently, in my program, I have accepted the user's input and implemented a method that returns an error message if the inputted format is invalid.
Any tips on where to go from here? Advice is greatly appreciated-- thank you!
If you are using Java 8 you can use java.time API like so :
String input = "201901151500";
LocalDateTime dt = LocalDateTime.parse(input, DateTimeFormatter.ofPattern("uuuuMMddHHmm"));
String output = dt.format(DateTimeFormatter.ofPattern("hh:mm a MMMM dd, uuuu"));
>> output = 03:00 PM janvier 15, 2019
Use the DateTimeFormatter as defined here: https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html
Pay attention to the parse method. You can define a formatter that takes in a string and then returns it in a certain way, almost any way you choose.
Here is the LocalDataTime class:
https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html
Example code:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy MM dd");
LocalDateTime local = LocalDateTime.parse("2004 12 25", formatter);
This question already has answers here:
want current date and time in "dd/MM/yyyy HH:mm:ss.SS" format
(11 answers)
Change the format of Date Java [closed]
(2 answers)
Closed 4 years ago.
I have the following piece of code:
System.out.println(array[9]);
Date d = df.parse(array[9]);
System.out.println(d.toString());
and the result of this looks like the following:
01.01.2017
Sun Jan 01 00:00:00 CET 2017
My DateFormatter:
DateFormat df = new SimpleDateFormat("dd.MM.yyyy",Locale.GERMANY);
So my question is now why I get the wrong format.
First result is a string, which I must convert to date.
But I got the wrong format, not the German one (dd.MM.yyyy).
What's wrong?
In your example you should use df.format(d) if you plan to convert Date to String. The default Date.toString() method will use the predefined format which you can't control.
This question already has answers here:
java date format yyyy-mm-dd.hh.MM.ss.ms
(3 answers)
Closed 5 years ago.
I am trying to parse the date from a given String. My input string is 2017-08-11, which represents Aug 11th 2017 (Basically yyyy-mm-dd format)
However if I run
new SimpleDateFormat("yyyy-mm-dd").parse("2017-08-11")
I get Wed Jan 11 00:08:00 GMT 2017
Why is this? I have created a test program at: http://ideone.com/dHe0ZA
M (capital M) represents month and m (small m) represents minute in java.
So the format you have specified is wrong.
Try
new SimpleDateFormat("yyyy-MM-dd").parse("2017-08-11")
You can see more details about the formats here.
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.
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.