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 6 years ago.
Improve this question
I need to use Calendar class. This is my code, but it has an error certainly. How can I fix it?
String stringdate = "20161129";
String pattern = "yyyyMMdd";
Date date = new SimpleDateFormat(pattern).parse(stringdate);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
Your code looks good...
2 things can happen her:
you are not using the correct DAte class (import java.util.Date;)
you are not considering (catching or rethrowing ) the ParseException
in both cases you will get a compilation error...
like type mismatch
or unhandled exception
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
Date format coming from the database: 1629777600000
SimpleDateFormat simpleDateFormatter = new SimpleDateFormat("MM/dd/yyyy");
simpleDateFormat.format(1629777600000);
When using this simple date format I am not able to convert 1629777600000 to a viewable date
SimpleDateFormat simpleDateFormatter = new SimpleDateFormat("MM/dd/yyyy");
System.out.println(simpleDateFormatter.format(1629777600000L));
Print: 08/24/2021
But your code does not compile:
you initialize simpleDateFormatter but use simpleDateFormat (without ter)
1629777600000 is a Long, therefore it needs an L at its end
I think the L problem is just from modifiying the code for the stackoverflow question. But the variable name is maybe a real problem in your code.
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 does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
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.
Improve this question
I am a Java developer.
I have a question about the Java Calendar library class.
What is my mistake? Is it a bug in Java?
Please explain to me.
public static void main(String[] args) {
Calendar cal2 = Calendar.getInstance();
Calendar cal3 = Calendar.getInstance();
DateFormat dateFormat = new SimpleDateFormat("dd/MM/YYYY");
cal3.add(Calendar.DATE, -1);
String today = (dateFormat.format(cal2.getTime()));
String yesterday = (dateFormat.format(cal3.getTime()));
System.out.println(today);
System.out.println(yesterday);
}
Output:
01/01/2019
Picked up _JAVA_OPTIONS: -Xmx512M
31/12/2019
2019 is as expected in the first line, but I had expected 2018 in the last line.
Use yyyy(Year) for year instead YYYY(Week year). See the doc.
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.