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.
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 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 1 year ago.
Improve this question
I am new to JFrame. I just started learning about creating a Java project. I created an input field as a date and inserted a DateChooseCombo. I have 2 problems.
When I run the application dates in the calendar are invisible but, it shows the date which is selected in the box.
When I submit the form it gives an error as "Cannot format given Object as a Date"
The code for the date is as follows:
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
String addDate = dateFormat.format(txt_AddDate.getSelectedDate());
ps.setString(3 ,addDate);
The date format used is 04/30/2021.
Can anyone help me to solve these two problems?
Method getSelectedDate(), in class datechooser.beans.DateChooserCombo, returns a java.util.Calendar.
Method format, in class java.text.DateFormat (which is superclass of java.text.SimpleDateFormat and hence inherited by SimpleDateformat) requires a parameter of type java.util.Date. A Calendar is not a Date and that's why you are getting the error. Java cannot convert a Calendar to a Date.
However, class Calendar has method getTime which returns a Date.
So you need to change the second line of the code, that you posted in your question, to the following.
String addDate = dateFormat.format(txt_AddDate.getSelectedDate().getTime());
You can also refer to the following question (and answer) :
Can't get date from DateChooserCombo
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 used DateTimeFormatter.ofPattern('yyyy-MM-dd'T'HH:mm:ssZ')
and I get error like this
java.time.format.DateTimeParseException: Text '2020-04-13T12:05:54+0600' could not be parsed at index 19
String which i wanna parse is '2020-04-13T12:05:54+0600'
so how can i solve this? What pattern i need to use?
Your code is using LocalDate which only parses a date - not a date and time so you are getting an error when the parse finds the space after the date.
So you should be using LocalDateTime but LocalDateTime.parse(String) expects an ISO format date which is not the format you are using.
So you need to use a DateTimeFormatter to specify the format of your input string. Something like:
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSX");
LocalDateTime result = LocalDateTime.parse(convertDate, format);
Answer copied from greg449’s answer here
You can pass pattern of Like this way.
DateTimeFormatter.ofPattern("yyyy MM dd");
Please follow the documentation to pass proper pattern.
https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html
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 am trying to build a program that libraries can use. The user inputs the date they loaned the book, and it calculates that date + 5 days (when the book is due). Then checks if the current date is before or after the due date. Then says if it is late or not.
Use SimpleDateFormat to take the user input (as a String), and
convert it to a Date. Use Calendar for adding days to a given Date.
See also:
SimpleDateFormat
Date
Calendar
Use JodaTime instead and then you can call plusDays() on a DateTime object. There is also an isBefore() method as well. Here is a simple example:
DateTime loanDate = new DateTime();
DateTime dueDate = new DateTime().plusDays(8);
DateTime date = loanDate.plusDays(5);
System.out.println(date.isBefore(dueDate));
This returns true as the date is before the due date.
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.