Android 如何从字符串中提取子字符串?

Android 如何从字符串中提取子字符串?,android,string,contacts,Android,String,Contacts,事实上,我必须从联系人那里找到今天的生日。我有一个字符串变量,比如 String value=“2014-06-24”。 我想得到字符串变量的月份和日期,与今天的日期进行比较,以找到生日。或者它还有其他方法吗?使用SimpleDataFormat,如果您没有得到,可以使用下面给出的方法 String[] arr = yourStringDate.split("-"); String year = arr[0]; String month = arr[1]; String day = arr[2]

事实上,我必须从联系人那里找到今天的生日。我有一个字符串变量,比如
String value=“2014-06-24”。


我想得到字符串变量的月份和日期,与今天的日期进行比较,以找到生日。或者它还有其他方法吗?

使用SimpleDataFormat,如果您没有得到,可以使用下面给出的方法

String[] arr = yourStringDate.split("-");
String year = arr[0];
String month = arr[1];
String day = arr[2];

使用SimpleDataFormat,如果您没有得到,可以使用下面给出的方法

String[] arr = yourStringDate.split("-");
String year = arr[0];
String month = arr[1];
String day = arr[2];

您可以使用
SimpleDataFormat
解析如下日期:

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();
}
if(dateA.after(dateB)) {
    // dateA is after dateB
} else {
    // dateA is before dateB
}
当您有
Date
对象时,您可以将它们与
before()
after()进行比较,如下所示:

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();
}
if(dateA.after(dateB)) {
    // dateA is after dateB
} else {
    // dateA is before dateB
}
您可以使用
日历
对象从
日期
对象获取更多信息

Calendar calendar = Calendar.getInstance();
calendar.setTime(date);

int month = calendar.get(Calendar.MONTH) + 1;
int day = calendar.get(Calendar.DAY_OF_MONTH);

您可以使用
SimpleDataFormat
解析如下日期:

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();
}
if(dateA.after(dateB)) {
    // dateA is after dateB
} else {
    // dateA is before dateB
}
当您有
Date
对象时,您可以将它们与
before()
after()进行比较,如下所示:

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();
}
if(dateA.after(dateB)) {
    // dateA is after dateB
} else {
    // dateA is before dateB
}
您可以使用
日历
对象从
日期
对象获取更多信息

Calendar calendar = Calendar.getInstance();
calendar.setTime(date);

int month = calendar.get(Calendar.MONTH) + 1;
int day = calendar.get(Calendar.DAY_OF_MONTH);

您可以使用以下方法从值字符串中查找日期和月份:

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

然后可以使用日期比较,如:


if(date.before(today)| | date.after(today))
等用于下一个操作。

您可以使用以下方法从值字符串中查找日期和月份:

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

然后可以使用日期比较,如:



if(date.before(today)| | date.after(today))
等进行下一步操作。

使用SimpleDataFormat,不要对日期类型执行字符串操作请尝试使用“-”进行拆分,月索引为(1),日索引为(2)。为什么有人要拆分
字符串
SimpleDataFormat
更简单,更不容易出错。请使用SimpleDataFormat,不要对日期类型执行字符串操作请尝试使用“-”进行拆分,月索引为(1),日索引为(2)。为什么有人要拆分
字符串
SimpleDataFormat
更简单,更不容易出错。不知道谁投了赞成票。。?我认为这是一个很好的解决方案。谢谢你…+1我不是这个问题的作者。除了数组格式,它还有任何格式可以存储你想做什么?不知道谁投了赞成票。。?我认为这是一个很好的解决方案。谢谢你…+1我不是这个问题的作者。除了数组格式,它还有任何格式可以存储你想做什么?我对此有疑问。从指定日期算起的月份是06。但本月的数据为6。我不明白你的意思,好吧。你从int month=calendar.get(calendar.month)+1中得到了什么值;它以整数形式返回当前月份。您不需要执行任何操作。只需将所有日期存储为
Date
对象,并将它们与
before()
before()
进行比较,就像我在回答中演示的那样。我对此有疑问。从指定日期算起的月份是06。但本月的数据为6。我不明白你的意思,好吧。你从int month=calendar.get(calendar.month)+1中得到了什么值;它以整数形式返回当前月份。您不需要执行任何操作。只需将所有日期存储为
Date
对象,并将它们与
before()
after()
进行比较,就像我在回答中演示的那样。