需要使用stringTokenizer格式化字符串“18-MAY-92”并获取月份的int值[复制]

问题描述 投票:-2回答:1

我正在尝试使用字符串标记生成器格式化作为字符串输入的日期。日期格式将输入为

String str="18-AUG-92".

我把它分成了

int ndate = Integer.parseInt(str.nextToken());
int nmonth = Integer.parseInt(str.nextToken());
int nyear = Integer.parseInt(str.nextToken());

但是在nmonth期间它显示错误,输入值“AUG”是字符串。

我想将“AUG”转换为08。

有人知道吗?

java date date-parsing
1个回答
0
投票

这个给你。

String datetime="18-Aug-92";
DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("dd-MMM-yy");
LocalDate date = LocalDate.parse(datetime, dateFormat);
System.out.println(date.getMonth().getValue());
© www.soinside.com 2019 - 2024. All rights reserved.