将月份从01,02改为1,2 [英] Getting months as 01,02 instead of 1,2

查看:148
本文介绍了将月份从01,02改为1,2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Calender类,更具体地说,我需要将所有的12个月作为两个数字取回。

I am working with the Calender class and more specifically I need to get back all 12 months as two numbers.

如果我使用以下代码:

 int month = (myCalendar.get(Calendar.MONTH)) +1;

这是我在不同月份得到的:
1,2,3,4 ,5,6,7,8,9,10,11,12

This is what I get for the different months: 1,2,3,4,5,6,7,8,9,10,11,12

但是我真正需要的是:
01,02,03,04, 05,06,07,08,09,10,11,12

But what I really need is: 01,02,03,04,05,06,07,08,09,10,11,12

这是因为我需要创建子字符串,并且如果我得到的整数数量会出错。
是否有解决此问题的简便方法?

This is beacuse I need to make substrings and it will go wrong if the number of ints I get. Is there an easy way to fix this?

我可以使用if语句检查整个日期是否只有5个数字,然后添加零并3位,但感觉很麻烦。

I could use an if statement and check if the whole date is just 5 numbers, then add a zero and the 3 position, but it feels cumberstone.

推荐答案

将其加载为 1而不是 01的原因是它存储在 int 中,如果执行以下语句,请执行以下操作:

The reason it is loading as "1" instead of "01" is because it is stored in an int, do the following if statement:

String sMonth = "";
if (month < 10) {
    sMonth = "0"+String.valueOf(month);
} else {
    sMonth = String.valueOf(month);
}

然后使用 sMonth 而不是

这篇关于将月份从01,02改为1,2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆