0

我的代码中有什么问题,方法总是返回当前日期

我只想更改日期格式

我错过了什么?

...
String mydate;

mydate=ConvertToDate("2013/09/25");



  private String ConvertToDate(String dateString){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date convertedDate = new Date();
        try {
            convertedDate = dateFormat.parse(dateString);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        String newdate;
        newdate =  dateFormat.format(convertedDate);
        return newdate;
    }
4

4 回答 4

2

上面带有 throws parseException 的代码更改为:

 private static  String ConvertToDate(String dateString){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
        Date convertedDate = new Date();
        try {
            convertedDate = dateFormat.parse(dateString);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        String newdate;
        SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");

        newdate =  dateFormat1.format(convertedDate);
        return newdate;
    }
于 2013-09-20T06:43:16.730 回答
0

你选择了错误的模式。您的日期是 2013/09/25,所以您的模式应该是 yyyy/MM/dd。

在您的代码中给出异常并且在 try catch 块之前您使用 new Date() 初始化了您的日期。因此,在您的 convertDate 变量出现异常后显示当前日期。

你必须改变

new SimpleDateFormat("yyyy-MM-dd") 

new SimpleDateFormat("yyyy/MM/dd")

您在评论中的回答。我在这里写给你。

private static String ConvertToDate(String dateString){
      SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
      SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy/MM/dd");
      Date convertedDate = new Date();
      try {
          convertedDate = dateFormat.parse(dateString);
      } catch (ParseException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }

      String newdate;
      newdate =  dateFormat2.format(convertedDate);
      return newdate;
  }
}
于 2013-09-20T06:37:22.063 回答
0
// Convert Date formate function
    public static String converDateFormate(String dateString) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("YYYY/mm/dd");
        Date testDate = null;
        testDate = sdf.parse(dateString);
        SimpleDateFormat formatter = new SimpleDateFormat("YYYY-mm-dd");
        String newFormat = formatter.format(testDate);
        System.out.println("" + newFormat);
        return newFormat;
    }

用这个:

converDateFormate("2013/12/20");
于 2013-09-20T06:40:48.553 回答
0

使用applyPattern,仅此而已

    String OLD_FORMAT = "yyyy/MM/dd";
    String NEW_FORMAT = "yyyy-MM-dd";


    String oldDateStr = "2013/09/25";
    String newDateStr;

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(OLD_FORMAT);
    Date date = simpleDateFormat.parse(oldDateStr);
    simpleDateFormat.applyPattern(NEW_FORMAT);
    newDateStr = simpleDateFormat.format(date);

    System.out.println(newDateStr);

输出:

2013-09-25
于 2013-09-20T06:51:12.810 回答