How do you input initialize and add days to dates? (java) [closed] - java

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.

Related

Why it shows errors at swing components [closed]

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

Java LocalDateTime pattern [closed]

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

how can i get day of month of a date in java to be use in gantt chart [closed]

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 3 years ago.
Improve this question
i want to get the day of month of a date in java
i try getday() but it return day of week
for (int i =0; i<count; i++) {
series1.add(new Task(Names.get(i),
Date.from(LocalDate.of(Dates.get(j).getYear(), Dates.get(j).getMonth(),Dates.get(j).getDay()).atStartOfDay().toInstant(ZoneOffset.UTC)),
Date.from(LocalDate.of(Dates.get(k).getYear(),Dates.get(k).getMonth(),Dates.get(k).getDay()).atStartOfDay().toInstant(ZoneOffset.UTC))
)
);
j+=2;k+=2;
}
I looked into your previous Stack Overflow question for some context. Please don’t expect that Stack Overflow users will usually do this. They will not.
As I understand it, Task is org.jfree.data.gantt.Task from JFreeChart, and its constructor requires two java.util.Date arguments (start and end). You are getting java.sql.Date objects from your database.
While you cannot (reasonably) change JFreeChart, since JDBC 4.2 you can take advantage of getting modern LocalDate objects rather than old-fashioned java.sql.Date objects from your database:
while (rs.next()) {
String a = rs.getString("TITRE");
LocalDate startDate = rs.getObject("DATE DEBUT Prévi", LocalDate.class);
LocalDate endDate = rs.getObject("DATE FIN prévi", LocalDate.class);
series1.add(new Task(a,
Date.from(startDate.atStartOfDay().toInstant(ZoneOffset.UTC)),
Date.from(endDate.atStartOfDay().toInstant(ZoneOffset.UTC))
)
);
}
I found it simpler not to put your fetched data into separate lists and taking it out again before constructing the Task objects. In your code please do as you find best.
PS The getXxx methods of java.sql.Date and java.util.Date are deprecated because they work unreliably across time zones, so even if you couldn’t avoid those classes, never call those methods. If you do get a java.sql.Date from somewhere, the first thing to do is call its toLocalDate​ method to convert it to a modern LocalDate.
EDIT: You are doing very much in a single statement with nested method calls. I agree with the comment by Basil Bourque that this can be hard for a reader (and yourself?) to follow. I believe that this was also part of why people (including myself) didn’t understand your question. We suggest breaking it up. One example would be:
Instant startInstant = startDate.atStartOfDay().toInstant(ZoneOffset.UTC);
Date startUtilDate = Date.from(startInstant);
// Similarly for end date
Task newTask = new Task(a, startUtilDate, endUtilDate);
series1.add(newTask);
It’s wordier, but each step is easy to follow now.
Instead of startDate.atStartOfDay().toInstant(ZoneOffset.UTC) I usually do startDate.atStartOfDay(ZoneOffset.UTC).toInstant(), but that’s a matter of taste or habit. The result is the same in both cases.
Links
Your previous Stack Overflow question: how can i draw gantt chart with date from database in java
Documentation of the org.jfree.data.gantt.Task(String, Date, Date) constructor
I am assuming Dates is a Map so Dates.get(key) returns a particular date stored in the map.
In this case you can simply use the java.util.Calendar to get the day of the month as an integer from your dates.
Date dt = Dates.get(key);
Calendar cal = Calendar.getInstance();
cal.setTime(dt);
int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH);
Hope it helps!

Convert int to date in Java/Android [closed]

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.

Add Day and Time parts to a Date Object [closed]

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)));

Categories