gpt4 book ai didi

android - 将字符串日期转换为整数并获取月份

转载 作者:太空狗 更新时间:2023-10-29 16:00:11 26 4
gpt4 key购买 nike

我想将字符串日期转换为整数并从该整数中获取月份,我该怎么做??

例如:我的字符串日期为:

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com