Actually i have to find today's birthday from contacts. I have the string variable like String value="2014-06-24".
I want to get the month and date of the of the string variable to compare with today date to find the birthday. Or it has any other methods?
Use SimpleDateFormate, if you don not get you can use bellow given method
String[] arr = yourStringDate.split("-");
String year = arr[0];
String month = arr[1];
String day = arr[2];
You can use SimpleDateFormat
to parse dates like this:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
Date date = dateFormat.parse("2014-06-24");
} catch (ParseException e) {
// This exception occurs when the String could not be parsed!
e.printStackTrace();
}
When you have Date
objects you can compare them with before()
and after()
like this:
if(dateA.after(dateB)) {
// dateA is after dateB
} else {
// dateA is before dateB
}
You can use a Calendar
object to get further information from the Date
object.
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int month = calendar.get(Calendar.MONTH) + 1;
int day = calendar.get(Calendar.DAY_OF_MONTH);
You can use following way to find date and month from value string:
Date date = System.currentTimeMillis();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
try {
date = formatter.parse(value);
} catch (ParseException e) {
// This exception occurs when the String could not be parsed!
e.printStackTrace();
}
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int birthDay = cal.get(Calendar.DATE);
int birthMonth = cal.get(Calendar.MONTH);
then
Calendar currentCal = Calendar.getInstance();
Date today = currentCal.getTime();
then you can use date comparison like :
if(date.before(today) || date.after(today))
and so on for your next operation.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.