2013-12-20 46 views
0

我已经成功地将日期字符串的格式从yyyy-MM-dd更改为MM/dd。现在的问题是,如果monthday的值都小于10,则格式应该是例如2/7而不是02/07。我该怎么做呢?如何在整数中省略0日期字符串

这里是我的代码:

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; 
    } 
+0

你解决了你的问题吗? – dipali

回答

2

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

formatter = new SimpleDateFormat("M/d"); 
+0

我正在寻找是否有其他解决方案,事实证明这与我现在所做的相似。所以我会检查这个解决方案。谢谢。 –

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)); 

我希望这是对你有用。

相关问题