0

我已成功将日期字符串的格式从yyyy-MM-dd更改为MM/dd。现在的问题是,如果monthday值都小于 10,则格式应该是 example2/7和 not 02/07。我该怎么做呢?

这是我的代码:

    public static String cahngeFormat(String dateTime) {
        Date date = null;
        String str = dateTime;
        DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        try {
            date = formatter.parse(str);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.HOUR, 2);
            formatter = new SimpleDateFormat("MM/dd");
            str = formatter.format(calendar.getTime());
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return str;
    }
4

2 回答 2

2

将格式从更改"MM/dd""M/d"改为允许单位数输出。

formatter = new SimpleDateFormat("M/d");
于 2013-12-20T04:19:01.423 回答
1
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTimeInMillis(System.currentTimeMillis());
                    String timer = formatter.format(calendar.getTime());
                    formatter.setTimeZone(java.util.TimeZone.getTimeZone("GMT"));
                    txtTimerCount.setText(formatter.format(timePassed));

我希望这对你有用。

于 2013-12-20T04:08:54.737 回答