我想将字符串日期转换为整数并从该整数中获取月份,我该怎么做??
例如: 我的字符串日期为:
String date = "15-06-2016";
那么我怎样才能得到月份:
06 as output in integer
最佳答案
使用 SimpleDateFormat 类,与将字符串转换为整数后相比,您只获得字符串中的月份
字符串日期字符串 = "15-06-2016"
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH);
try {
Date date = sdf.parse(dateString);
String formated = new SimpleDateFormat("MM").format(date);
int month = Integer.parseInt(formated);
} catch (Exception e) {
e.printStackTrace();
}
关于android - 将字符串日期转换为整数并获取月份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37803686/