How to check if a date is after today java
As mentioned in comments, it's not possible that Date object will have null reference. However if sdf.parse(date) throws an exception which is suppressed then enteredDate could be null.
String date="2016/04/26";
Date enteredDate=null;
try
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
enteredDate = sdf.parse(date);
}catch (Exception ex)
{
// enteredDate will be null if date="287686";
}
Date currentDate = new Date();
if(enteredDate.after(currentDate)){
System.out.println("after ");
}else
System.out.println("before");
Check date with todays date
Does this help?
Calendar c = Calendar.getInstance();
// set the calendar to start of today
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
// and get that as a Date
Date today = c.getTime();
// or as a timestamp in milliseconds
long todayInMillis = c.getTimeInMillis();
// user-specified date which you are testing
// let's say the components come from a form or something
int year = 2011;
int month = 5;
int dayOfMonth = 20;
// reuse the calendar to set user specified date
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month);
c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
// and get that as a Date
Date dateSpecified = c.getTime();
// test your condition
if (dateSpecified.before(today)) {
System.err.println("Date specified [" + dateSpecified + "] is before today [" + today + "]");
} else {
System.err.println("Date specified [" + dateSpecified + "] is NOT before today [" + today + "]");
}
How to check string date is in today?
Use the java-8 date time API, first your formatter is wrong month should represent with upper case letters MM
, and use DateTimeFormatter instead of legacy SimpleDateFormat
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
And then parse the input string into LocalDateTime using formatter
String date = "2019-12-07 20:13:04";
LocalDateTime dateTime = LocalDateTime.parse(date,formatter);
Finally compare the input date with current date using equals
, here is the information to get java-8 date time API on android
dateTime.toLocalDate().equals(LocalDate.now()); //true
Check if date is one week before from today Java
Using only Date
and Calendar
classes (and thus, compatible with any JVM from about 4-ish, I think?) you could try this sort of solution:
- Get today as a
Date
:Date now = new Date()
- Get one week ago from that, as a
Calendar
:Calendar expected = Calendar.getInstance(); expected.setTime(now); lastWeek.add(Calendar.WEEK_OF_YEAR, -1);
- Get your expired date as a
Calendar
:Calendar actual = Calendar.getInstance().setTime(expiredDate);
- Compare the year and day of year of the two calendars (you can compare other fields, but those two should be enough):
return (expected.get(Calendar.YEAR) == actual.get(Calendar.YEAR)) && (expected.get(Calendar.WEEK_OF_YEAR) == actual.get(Calendar.WEEK_OF_YEAR));
Using this, you should be able to come up with an even shorter snippet that subtracts a week from now and compares the long values of the two. Though obviously that wouldn't be comparing calendar dates, it would be comparing nanoseconds :)
To check if the date is after the specified date?
Apart from the logic error that the assylias pointed out:
You don't specify a time so givenDate
is "15/02/13 00:00:00.000" in your example. Now, if you compare that given date to any real timestamp on the same day (aka "now"), then the givenDate
is almost always "older" then "now".
Set givenDate
to the last millisecond of that day and you won't need the "equals" test.
How to check if a certain date is passed
Using @Mifmif answer I finally solved the problem with:
if (new SimpleDateFormat("MM/yyyy").parse(date).before(new Date())) {
...
}
Related Topics
How to Get the Average of a 2D Array
Setonclicklistener() on a Null Object Reference Error
Error: Incompatible Types: Int[][] Cannot Be Converted to Int
How to Check If a Json Key Exists
Convert Localdatetime to Localdatetime in Utc
Find Everything Between Two Xml Tags With Regex
Remove Trailing Comma from Comma-Separated String
Better Way to Detect If a String Contains Multiple Words
How to Parse Json Boolean Value
Java.Lang.Illegalargumentexception: Not a Managed Type: Class
How to Do a Limit Query in Jpql or Hql
In Java: How to Zip File from Byte[] Array
Check and Open Downloaded File in Selenium
Splitting Data Inside Quotes and Comma Using Regex
Eclipse: the Resource Is Not on the Build Path of a Java Project
The Request Was Rejected Because No Multipart Boundary Was Found in Springboot
Base64: Java.Lang.Illegalargumentexception: Illegal Character