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.
Related
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 11 months ago.
Improve this question
I'm trying to parse an String into a java.util.Date.
Currently, I'm using SimpleDateFormat, with the "yyyy-MM-dd'T'HH:mm:ss.SSSXXX" format String, and it works pretty well most of the time; for example, those work okay:
"2022-03-16T12:09:56.267Z"
"2022-03-16T12:11:55.017+03:00"
The problem lies with perfectly valid ISO strings that happen to use less than three digits for the miliseconds:
"2022-03-16T09:18:31.9Z"
It throws this exception: java.text.ParseException: Unparseable date: "2022-03-16T09:18:31.9Z".
Is there a way to handle those? Please, do keep in mind that I need to return a java.util.Date, but using SimpleDateFormat is optional.
I'm using Java 8.
Here is one way.
Note the Z stands for Zulu.
And also remember that Date does not store any time zone information.
If necessary, you can modify the ZonedDateTime instance before converting to Date.
Instant d = Instant.parse("2022-03-16T09:18:31.9Z");
Date date = Date.from(d);
System.out.println(d);
System.out.println(date);
prints
2022-03-16T09:18:31.900Z
Wed Mar 16 05:18:31 EDT 2022
I would recommend that you try to convert to using the classes in the java.time package as they are quite superior to Date.
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 4 years ago.
Improve this question
I'm a bit baffled what format these timestamps are in. I was told the format to use is yyyy-MM-dd.HH:mm:ss but all of the timestamps appear like this 2017-01-01 00:08:57.231, 2017-01-01 07:43:36.348, or 2017-01-01 13:25:55.683. I'm not understanding why there are four sections to the time ?:Hour:Minute:Second in the actual data I have when the format I'm supposed to be using only has three time sections. Are these datetime timestamps not actually in the format of yyyy-MM-dd.HH:mm:ss?
No, your suspicion is correct, your example date-time strings are not in the format yyyy-MM-dd.HH:mm:ss. The dot between dd and HH must be a simple mistake, it should be a space since there is a space between date and time in the timestamp strings. Furthermore all of your example strings include milliseconds: in 00:08:57.231 you’ve got 57 seconds and 231 milliseconds, or if you like, 57.231 seconds, so the last section may also be referred to as fraction of second.
DateTimeFormatter formatter
= DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSS");
String timestampString = "2017-01-01 00:08:57.231";
LocalDateTime dateTime = LocalDateTime.parse(timestampString, formatter);
System.out.println(dateTime);
Output:
2017-01-01T00:08:57.231
For the nerdily curious: It is possible to parse the string, or more precisely most of it, with the format you had been given, only correcting the dot into a space:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String timestampString = "2017-01-01 00:08:57.231";
LocalDateTime dateTime = LocalDateTime.from(
formatter.parse(timestampString, new ParsePosition(0)));
In this case the result comes without the milliseconds:
2017-01-01T00:08:57
I see no reason why you should want this, though.
Avoid SimpleDateFormat
In a comment you gave a snippet that uses the SimpleDateFormat class. This class is not only long outdated, it is also notoriously troublesome. I see no reason why you should want to use it. Instead I am using java.time, the modern Java date and time API. In my experience it is so much nicer to work with.
Links
Oracle tutorial: Date Time explaining how to use java.time. You may especially want to study the section Parsing and Formatting.
These time stamps are in yyyy-MM-dd HH:mm:ss:ms format, last three digits are milliseconds.
This question already has answers here:
Change date format in a Java string
(22 answers)
Closed 7 years ago.
Wha would be the equivalent in Java for this C# code:
DateTime.Now.TimeOfDay //This
DateTime.Now.ToString("h:mm:ss tt") //Or This
I'm finding a lot of things, some of them may be deprecated because it has 2 years or more, others are really complex for such a simple thing, and I'm sure there is a simpler way to do so.
I tried:
import java.util.Date;
Date d = new Date();
System.out.println(d); //Result: Wed Oct 28 00:46:29 2015
If I try:
System.out.println(d.getTime()); //Result: 1446000426285
I need to get only time: H:mm:ss
You can use SimpleDateFormat in java. date.getTime() gives you the milliseconds since January 1, 1970, 00:00:00 GMT.
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm:ss");
System.out.println(dateFormat.format(date));
Generally speaking, in Java, date/time objects are just a container for the amount of time which has passed since a given point in time (like the Unix epoch), so they tend not to have there own concept of formatting.
Formatting is normally managed by a supporting API, like java.text.DateFormat or java.time.format.DateTimeFormatter
If you're using Java 8, you could start with LocalTime
LocalTime time = LocalTime.now();
System.out.println(DateTimeFormatter.ISO_LOCAL_TIME.format(time));
which prints 13:51:13.466.
If you need a specific format, you can supply your own pattern to DateTimeFormatter, something like System.out.println(DateTimeFormatter.ofPattern("h:mm:ss a").format(time)); which prints 1:52:31 PM
Have a look at Date and Time Classes for more details
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 8 years ago.
Improve this question
I'm trying to get data from a website, and when I tried to get the date of a post (expected: 13/06/2014 11:55), i got:
23377855
Can someone help me to convert this number to a date? Thanks!
You can use the standard Java Date API:
long yourNumber = 23377855;
Date date = new Date(yourNumber);
Or you can use Joda Time library, provides much better overall functionality than Java Date API:
long yourNumber = 23377855;
DateTime dt = new DateTime(yourNumber);
Java is expecting milliseconds:
java.util.Date time= new java.util.Date((long)urDateNum*1000);
So you must multiply by 1000
Docs say:
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.
Note:
The cast to long is very important in this situation. Without it the integer overflows.
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)));