Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
How to convert Object as timestamp to formatted date?
I have printed out timestamp(
1395500668
) that is in Object.
I want to format this and print it out like
yyyy-MM-dd H:m:s
Assuming you have a string that represents epoch time in seconds (as you have not told us what your Object actually is), first convert it to a long:
long epoch = Long.parseLong("1395500668");
You'll then need to convert it to milliseconds:
epoch *= 1000;
Then you can convert it to a java.util.Date using the Date constructor that takes millisecond epoch time:
Date date = new Date(epoch);
And finally you can format that Date as a string using standard formatting techniques.
First convert the timestamp value into Date and then format the date into your desired format using SimpleDateFormat
java.util.Date date = new java.util.Date(new Long(1395500668) * 1000);
String dateStr = new java.text.SimpleDateFormat("yyyy-MM-dd H:m:s").format(date);
System.out.println(dateStr);
It outputs:
2014-03-22 8:4:28
Convert it first into a Date first by casting the Object into a Timestamp and then using the getTime() method as shown in How to convert TimeStamp to Date in Java?
Then use a SimpleDateFormat to format the date as needed as shown in Change date format in a Java string
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
how to convert ISO_LOCAL_DATE to date time format : yyyy-MM-dd'T'HH:mm:ss.SSSZ in java
Ex: given date: 2016-01-25 to 2016-01-25T00:00:00.000+0100
I am assuming that have got a string, for example 2016-01-25, and that you want a string containing the start of the day in the JVM’s default time zone (it wasn’t clear from the question). I first define a formatter for the format that you want (it’s ISO 8601):
private static DateTimeFormatter formatter
= DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSxx");
Now your conversion goes:
String isoLocalDateString = "2016-01-25";
LocalDate date = LocalDate.parse(isoLocalDateString);
ZonedDateTime dateTime = date.atStartOfDay(ZoneId.systemDefault());
String dateTimeString = dateTime.format(formatter);
System.out.println(dateTimeString);
When running in my time zone, Europe/Copenhagen, output from this example code is what you asked for:
2016-01-25T00:00:00.000+0100
In rare cases where summer time (DST) begins at the first moment of the day, the time of day will not be 00:00:00.000.
For parsing with ISO_LOCAL_DATE we don’t need to specify the formatter since this formatter is the default for LocalDate.parse().
All of this said, you should not normally want to convert a date from one string format to another string format. Inside your program keep dates as LocalDate objects. When you get string input, parse into a LocalDate. Only when you need to give string output, for example in data exchange with another system, format into a string in the required format.
Link: Wikipedia article: ISO 8601
There are various methods on LocalDate for this, including:
LocalDate::toDateTimeAtCurrentTime()
LocalDate::toDateTimeAtStartOfDay()
LocalDate::toDateTime( LocalTime )
LocalDate::toDateTime( LocalTime , DateTimeZone )
It is as simple as LocalDateTime localDateTime = yourLocalDate.atStartOfDay()
Update
Adding timestamp is as simple as:
ZoneId zoneId = ZoneId.of("America/New_York");
ZonedDateTime = zdt = localDateTime.atZone(zoneId);
Can be put together as
ZonedDateTime zdt = yourLocalDate.atStartOfDay().atZone(ZoneId.of("America/New_York"));
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have a String value as follows
2018-12-05 18:11:27.187
How can I can convert it into a LocalDate object based on that format "MM/dd/YYYY HH:mm:ss" ?
a LocalDate object is what it is. It has no format; it is an object with methods; these methods make it do stuff.
You can for example ask a localdate to return the year of the date it represents. You can also ask it to render itself as a string using some format. That string is then not a LocalDate (it is a String).
Furthermore, a localdate represents a date. Hence the name. 'hour' is not part of a date. YYYY is the pattern for week based year. You don't want that.
So, fixing your misconceptions, we end up with:
DateTimeFormatter inFormat = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSS");
DateTimeFormatter outFormat = DateTimeFormatter.ofPattern("MM/dd/uuuu HH:mm:ss");
LocalDateTime when = LocalDateTime.parse("2018-12-05 18:11:27.187", inFormat);
System.out.println(outFormat.format(when));
Firstly you need to define the pattern of your current Date and Time input
Parse the current Date and Time to LocalDateTime class
Print the value to the new Date and Time format you want.
LocalDateTime date = LocalDateTime.parse("2018-12-05 18:11:27.187", DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSS"));
System.out.println(date.format(DateTimeFormatter.ofPattern("MM/dd/uuuu HH:mm:ss")));
This question already has answers here:
How to convert currentTimeMillis to a date in Java?
(13 answers)
Closed 5 years ago.
I did some searched on web about BigInt data conversion into date while I've found plenty of same question but non of them seems to work. There is 13 digit's data 1435555326831 into my database and I think this is UNIXTIME, now I want to convert it into yyyy-MM-dd HH:mm:ss form. Thank you
You can first convert the number into a long (if you receive a BigInteger, you can call BigInteger.longValue()).
Then you have two options. With the standard java.util.Date you can use:
long millis = 1435555326831L;
Date d = new Date(millis);
You can then format the date with a SimpleDateFormat for output.
If you can use Java 8's new Time API, you can create an instant and convert it to the desired time zone (your computer time zone in my example below):
Instant instant = Instant.ofEpochMilli(millis);
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
System.out.println(fmt.format(instant.atZone(ZoneId.systemDefault())));
Note that these conversions only work if BigInteger is smaller than the maximum long size, not in general. This shouldn't be an issue, since the maximum value of a long is 2^63 - 1, but if your BigInteger is user input, you need to check for this.
Your data is on Unix timestamp and you can simply convert it by using new java.util.Date()
and here is the example of it
Java: Date from unix timestamp
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
long beginupd = new GregorianCalendar(2014,3,14,10,55,25).getTime().getTime();
Date date = new Date();
long milli=date.getTime();
System.out.println(beginupd);
System.out.println(milli);
System.out.println(date);
output:
1397462125000
1394787327009
Fri Mar 14 10:55:27 EET 2014
What is my wrong? why is it not equal? difference onyl two second but output difference very large
OK!
0 for January and 11 for December. thank you David Wallace
If it is not already a Date, parse it into a Date. The date format is arbitrary as long as you can construct an appropriate SimpleDateFormat to represent it.
After you have a Date, you can use Date.getTime() to retrieve the millisecond value.
For the example you have shown, if you have a string:
String datestr = "2014-14-03 01:39:00";
Then the matching SimpleDateFormat would be:
DateFormat format = new SimpleDateFormat("yyyy-dd-MM HH:mm:ss");
And conversion would be:
long millis = format.parse(datestr).getTime();
It's no problem to use Date for this, as the constructors and getTime() are still some of the few remaining non-deprecated components.
Edit: I see that you have edited your question to include the use of Date. The constructor you are using is deprecated, and is also not very flexible wrt. input (you have to have the date components already parsed to use it). A SimpleDateFormat provides a non-deprecated way to convert arbitrary strings to dates.
The reason this doesn't work is because the deprecated Date constructor that you're using expects year - 1900 as the first argument.
You should either use a SimpleDateFormat or a GregorianCalendar to do this conversion instead. Since there is already an excellent answer here, showing the use of SimpleDateFormat, here's how you use GregorianCalendar for 1:39am on 14 March 2014.
new GregorianCalendar(2014, 2, 14, 1, 39, 0).getTime().getTime();
Beware that the month uses 0 for January and 11 for December.
There is a nice article on the Date APIs that can be found here.
http://www.mkyong.com/java/java-time-elapsed-in-days-hours-minutes-seconds/
In order to convert to milliseconds, simply do some basic math.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have two date objects.
Date d1;
Date d2;
My program stores in d1, the date in the format 'dd/MM/yyyy' and in d2 in the format 'HH:mm'.
I would now like to combine these information and create a new Date object with the format 'dd/MM/yyyy HH:mm'.
Any pointers on how can I achieve so?
-V
If you are indeed using Date objects, just set the time of d1 to match that of d2.
d1.setHours(d2.getHours());
d1.setMinutes(d2.getMinutes());
Or make a new Date object if you don't want to use d1. Just parse it afterwards with SimpleDateFormat.
Note however that this is deprecated and it's better to use Calendar for this.
First, most of functionality of Date is deprecated. Use Calendar instead. Second, to parse and create string representation of date use SimpleDateFormat.
I hope these tips are enough to start. There are a lot of references in net and javadoc to continue. Good luck.
You cannot use Date for this purpose. You should instead use Calendar (from JDK) or joda-time.
try out this..
String dateValue= new java.util.Date().toString();
String concat like this..
String newDateTime = d1+""+d2+":00";
Try this
DateFormat df1=new SimpleDateFormat("dd/MM/yyyy");
DateFormat df2=new SimpleDateFormat("HH:mm");
Date d1=df1.parse("09/07/2013");
Date d2=df2.parse("14:43");
String d=df1.format(d1).toString()+" "+df2.format(d2).toString();
DateFormat newDate=new SimpleDateFormat("dd/MM/yyyy HH:mm");
System.out.println(newDate.format(newDate.parse(d)));