java date to integer,在Java中将Month String转换为Integer

这篇博客介绍了如何在Java中利用Joda-Time库将如'Feb'或'February'这样的月份字符串转换为对应的月份数字,方法是通过DateTimeFormat和DateTimeFormatter。示例代码展示了在法语环境下解析'août'并获取英文月份文本和数字的过程。
摘要由CSDN通过智能技术生成

Given a month string such as:

"Feb"

or

"February"

Is there any core Java or third party library functionality that would allow you to convert this string to the corresponding month number in a locale agnostic way?

解决方案

An alternative to SimpleDateFormat using Joda time:

import org.joda.time.DateTime;

import org.joda.time.format.DateTimeFormat;

import org.joda.time.format.DateTimeFormatter;

...

// if default locale is ok simply omit '.withLocale(...)'

DateTimeFormatter format = DateTimeFormat.forPattern("MMM");

DateTime instance = format.withLocale(Locale.FRENCH).parseDateTime("août");

int month_number = instance.getMonthOfYear();

String month_text = instance.monthOfYear().getAsText(Locale.ENGLISH);

System.out.println( "Month Number: " + month_number );

System.out.println( "Month Text: " + month_text );

OUTPUT:

Month Number: 8

Month Text: August

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值