This question already has answers here:
How to compare dates in Java? [duplicate]
(11 answers)
Time comparison
(12 answers)
Closed 2 years ago.
I'm working on a Java program where the user, at some point, give us as an input two times (in format String). We can assume the times are from the same day. For example in the console:
Please introduce time 1:
16:00
Please introduce time 2:
10:00
My program will capture those two time values as a String and my idea was to parse it into an object of class Date.
How could I calculate which time goes first in the course of a day?
For the previous example, I would like to print the first time that takes place on the day.
How can I properly do the following?
if (date1 <= date2) System.out.println(date1); //Suppose date1 and date2 are Date objects
else System.out.println(date2);
String firstDateString = "16:00";
String secondDateString = "10:00";
LocalTime firstLocalTime = LocalTime.parse(firstDateString, DateTimeFormatter.ofPattern("HH:mm"));
LocalTime secondLocalTime = LocalTime.parse(secondDateString, DateTimeFormatter.ofPattern("HH:mm"));
if (firstLocalTime.isAfter(secondLocalTime)) {
System.out.println(secondLocalTime);
} else {
System.out.println(firstLocalTime);
}
Related
This question already has answers here:
Is it the last day of the month?
(4 answers)
Closed 11 months ago.
My input string date is as below:
String date = "1/31/2022";
I am transforming to a date:
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
How do I know if my date is the last day of the month?
E.g.: for a String "1/31/2022" the output should be true. For 2/2/2023 it should be fast.
This is to run in Groovy in Jenkins but I believe I could use java as well
SimpleDateFormat is obsolete. I'd use the modern stuff from the java.time package.
Use single character M and d if not padding single-digit values with a leading zero.
DateTimeFormatter MY_FORMAT = DateTimeFormatter.ofPattern("M/d/uuuu");
LocalDate x = LocalDate.parse("1/31/2022", MY_FORMAT);
if (x.getDayOfMonth() == x.lengthOfMonth()) {
// it is the last day of the month
}
This question already has answers here:
Calculating the difference between two Java date instances
(45 answers)
Closed 12 months ago.
So I'm having some issues in trying to do this, I already tried with this code:
thatDay.set(Calendar.DAY_OF_MONTH,25);
thatDay.set(Calendar.MONTH,7); // 0-11 so 1 less
thatDay.set(Calendar.YEAR, 1985);
Calendar today = Calendar.getInstance();
long diff = today.getTimeInMillis() - thatDay.getTimeInMillis(); //result in millis
And I kinda know why this is not what I want because, the thing that I need is taking the first date that I insert in database, and I want that to be like (CURRENTDATE - TXTDATE == DAYS THAT LEFT)
Maybe try this:
public long daysBetween(Calendar startDate, Calendar endDate) {
return TimeUnit.MILLISECONDS.toDays(Math.abs(endDate.getTimeInMillis() - startDate.getTimeInMillis()));
}
This question already has answers here:
Java SimpleDateFormat always returning January for Month
(4 answers)
SimpleDateFormatter does not recognize months
(4 answers)
Closed 3 years ago.
I am building an app using hibernate(spring-jpa more specifically).
My model class contain a java.util.Date field and I want to filter the records based on the date field ignoring the time part.
I try to achieve this with spring-jpa specification but for equal operation it always returns 0 objects
I further debug this problem and found that when hibernate return the Object with field type is java.sql.timeStamp where I compare it with java.util.Date so it never matched.
Below is sample code I used for debugging
List<EmployeeLeaves> l = empLeaveDao.findAll();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-DD");
Date date = formatter.parse("2018-04-28");
long time = date.getTime();
date = new java.sql.Timestamp(time);
for (EmployeeLeaves d : l) {
System.out.println(d.getAppliedOn().getClass().getName());
if (d.getAppliedOn().equals(date)) {
System.out.println("==============" + d.getRecordId());
}
}
return l;
Still no luck as two date Objects never match as equals method never returns true.
Can anyone suggest how to create a new Date object so It can equal with Date objects returned by database.
You can use function Date.compareTo() as in this artile enter link description here
I also tested with this small code snippet, it works as expected
Date date = new Date();
java.sql.Date sqlDate = new java.sql.Date(date.getTime());
System.out.println(date.compareTo(sqlDate) == 0);
Also if you use java8, why don't you change the date type in Entity class to LocalDate and you would not have this kind of problem?
This question already has answers here:
Converting number representing a date in Excel to a Java Date object
(6 answers)
Closed 4 years ago.
Excel converts dates into 5 digits int so I'm asking for a way to convert this 5 digits int ex. 43277 to date String dd-MMM-yy and how to change it "date String" back to 5 digits int.
I'm trying to make the app reads and writes from an google spreadsheet.
It's true that a similar question asked here "Converting Number representation of Date in excel to Date in java" but the code provided did not work or me as it showed cannot resolve symbol 'DateUtil' and the i couldnt understand the second answer.
Also I am asking how to convert it back and forth not from int to String only.
Excel’s serialized dates are the number of days since 1/1/1900. In order to figure out the date again, we have to add the serial number worth of days.
Java 8 version
/*
1900-1-0 0
1900-1-1 1
1900-1-2 2
1900-1-3 3
*/
int days = 43323;
LocalDate start = LocalDate.of(1900, 1, 1);
LocalDate today = LocalDate.of(2018, 8, 11);
// days to date
LocalDate date = start.plusDays(days).minusDays(2);
System.out.println(date);
// date to days
long days1 = ChronoUnit.DAYS.between(start, today) + 2;
System.out.println(days1);
This question already has answers here:
Comparing two java.util.Dates to see if they are in the same day
(14 answers)
Closed 6 years ago.
I have a problem with Date instance. I did the following:
Date startDate = new Date(); //this is from database value
Date todayDate = new Date(); //this is created locally
Now when am trying to compare them, the only issue is that the Date instance will have time, so when I check if they are equal it probably wouldn't give the same thing I expect, but rather less or more. I tested the following:
System.out.println(rsv.startDate);
System.out.println("Today date:"+todayDate);
if(rsv.startDate.equals(todayDate)){
System.out.println("Equal!");
}else if(rsv.startDate.after(todayDate)){
System.out.println("After!!");
}else{
System.out.println("Before!!!!");
}
and although both are 5th feb but it shows output of Before instead of equal. How can I remedy this? I know about SimpleDateFormat but that would change the date to strings.
Thanks,
For Date operation you can use Joda utility. Following snippet code shows compare two date :
DateTime one = new DateTime(original-Date-1);
DateTime two = new DateTime(original-Date-2);
LocalDate oneDate = one.toLocalDate();
LocalDate twoDate = two.toLocalDate();
return oneDate.compareTo(twoDate);
You can see: http://joda-time.sourceforge.net/index.html
You can strip out the time from the current Date object:
Date date = new Date();
Calendar dCal = Calendar.getInstance();
dCal.setTime(date);
dCal.set(Calendar.HOUR_OF_DAY, 0);
dCal.set(Calendar.MINUTE, 0);
dCal.set(Calendar.SECOND, 0);
dCal.set(Calendar.MILLISECOND, 0);
date = dCal.getTime();
And then make your comparision.
Alternatively, if you need in your project more date/time processing power, you can use joda date time library: http://joda-time.sourceforge.net
MidnightDate class is suitable for this specific usecase: http://joda-time.sourceforge.net/api-release/org/joda/time/DateMidnight.html
Date contains both the date and time. Equals() will only return true if the time is also the same. To make the comparison work, you need to make sure that the time portion of both Date objects is the same (eg all set to zero).