Reputation: 107
I want to compare current date with the given date. Current date format will be (dd-MM-YYYY). I don't want to get the current year. Suppose today's date is 30-08-2016. I want to get only 30-08, I don't want current year 2016. The given date is 15-08 and 31-08. I did not get the current date like this 30-08. How can I get this and how can I compare? Here is my code which I have tried:
Date date1_aug, date2_aug, cur_date;
String cur_date_string, sDay, sMonth;
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-YYYY");
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH)+1;
int day = calendar.get(Calendar.DATE);
sDay = Integer.toString(day);
sMonth = Integer.toString(month);
cur_date_string = sDay+"-"+sMonth;
try {
cur_date = sdf.parse(cur_date_string);
} catch (java.text.ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
another function:
try{
date1_aug = sdf.parse("15-08");
date2_aug = sdf.parse("31-08");
if(cur_date.equals(date1_aug) || cur_date.after(date1_aug) || cur_date.before(date2_aug)){
textView_Amount_LateFee.setText(Config.fine_aug.get(1));
}
}catch(ParseException ex){
ex.printStackTrace();
}
Upvotes: 3
Views: 3311
Reputation: 339073
java.time.MonthDay
Use modern date-time classes in the ThreeTenABP project, an Android adaptation of the back-port of the java-time classes built into Java 8 and later. Avoid the troublesome old legacy date-time classes.
These java.time classes include the MonthDay
class to represent a month and a day-of-month without a year.
MonthDay md = MonthDay.of( 8 , 15 );
This class already implements the Comparable
interface and its compareTo
method. Also implements methods equals
, isBefore
, and isAfter
. So, job done.
Determining the current MonthDay
requires a time zone. For any given moment the date varies by time zone.
ZoneId z = ZoneId.of( "America/Montreal" );
MonthDay today = MonthDay.now( z );
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
Upvotes: 1
Reputation: 5629
If you want to ignore the year, forget about using the date concept itself.
String given_date = "15-08";
String[] tokens = given_date.split("-");
int given_day = Integer.parseInt(tokens[0]);
int given_month = Integer.parseInt(tokens[1]);
Calendar c = Calendar.getInstance();
int now_month = c.get(Calendar.MONTH) + 1;
int now_day = c.get(Calendar.DATE);
System.out.println(given_day + " " + given_month + " " + now_day + " " + now_month);
if (now_month > given_month) {
System.out.println("Before Today!");
} else if (now_month < given_month) {
System.out.println("After Today!");
} else {
if (now_day > given_day) {
System.out.println("Before Today!");
} else if (now_day < given_day) {
System.out.println("After Today!");
} else {
System.out.println("Today!");
}
}
Upvotes: 0
Reputation: 389
function to check dob( with current date)
/** dob should be in the format yyyy-MM-dd hh:mm:ss */
public static boolean isTodayBdy(String dob) {
Date today = new Date();
try {
if(dob.length() > 1){
Date dobDate = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(dob);
if ((today.getMonth() == dobDate.getMonth()) && (today.getDate() == dobDate.getDate())) {
return true;
}
}
} catch (ParseException e) {
return false;
}
return false;
}
Upvotes: 0
Reputation: 98
Have you tried to use the same year in the dates you want to compare?
Just using Calendar, set the year to be the same in the two Date objects and after that compare them...
(Take care with the leap-years)
Upvotes: 0