java - 递增月份
问题描述
我有一个可以保存一个月的字符串。
String mon = "January";
nextMon
现在,下个月应该有另一个字符串。也就是说,nextMon
应该是"February"
if mon
is "January"
。
我mon
从函数中获取字符串,所以它必须自动发生。
请告诉我最简单的方法。
解决方案
正如 Slaw 所说,使用Month
一年中的一个月,而不是字符串。将你的字符串解析成Month
这样的:
DateTimeFormatter monthFormatter = DateTimeFormatter.ofPattern("MMMM", Locale.ENGLISH);
String monthAsString = "January";
Month mon = monthFormatter.parse(monthAsString, Month::from);
System.out.println(mon);
到目前为止的输出是:
一月
增加。只需添加 1:
mon = mon.plus(1);
System.out.println(monthFormatter.format(mon));
二月
推荐阅读
- c# - C# | DataGridView 值在末尾而不是开头
- sapui5 - 如何以编程方式触发智能字段值帮助的搜索?
- node.js - 在 Windows 上安装反应平台,出现“参数格式不正确”等错误
- php - “自动”语言更改
- python - 使用 scipy.sparse.linalg.eigsh 获取所有特征向量
- amazon-web-services - 删除与用户共享密钥
- android - 使用 String.format() 时更改字符串两部分的颜色
- parsing - 在解析期间添加运算符
- flask - 在烧瓶单元测试中路径 before_request 方法
- javascript - 无法发送推送通知 - 未能获取有效的 Google OAuth2 访问令牌