【问题标题】:How to convert date in to String MM/dd/yyyy Format? [closed]如何将日期转换为字符串 MM/dd/yyyy 格式? [关闭]
【发布时间】:2014-04-13 04:56:46
【问题描述】:

我有一个日期 2014-10-01 00:00:00.0 我必须转换为 10/01.2014 我该怎么办?

【问题讨论】:

  • 这样的“我该怎么做”的问题更像是“给我代码”的问题。看右边的相关问题。祝你好运!
  • 你为什么不搜索一下解决方案?这里有一些对你有帮助的东西:mkyong.com/java/java-date-and-calendar-examples
  • rajesh,你的问题解决了吗?
  • 是的,它的工作,非常感谢你:)

标签: java date simpledateformat date-formatting


【解决方案1】:

这个怎么样

try
 {
    String currentDate = "2014-10-01 00:00:00.0";
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
    Date tempDate=simpleDateFormat.parse(currentDate);
    SimpleDateFormat outputDateFormat = new SimpleDateFormat("dd/MM.YYYY");           
    System.out.println("Output date is = "+outputDateFormat.format(tempDate));
  } catch (ParseException ex) 
  {
        System.out.println("Parse Exception");
  }

【讨论】:

    【解决方案2】:

    使用 SimpleDateFormat:

    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
    String date = sdf.format("Your date");
    

    【讨论】:

      【解决方案3】:

      试试这个

      // Create an instance of SimpleDateFormat used for formatting 
        // the string representation of date (month/day/year)
        DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
      
       // Get the date today using Calendar object.
         Date today = Calendar.getInstance().getTime();        
          // Using DateFormat format method we can create a string 
        // representation of a date with the defined format.
        String reportDate = df.format(today);
      
         // Print what date is today!
      System.out.println("Report Date: " + reportDate);
      

      【讨论】:

        【解决方案4】:

        如果您使用 JodaTime 库,这很容易。

            DateTime dt = new DateTime();
            DateTimeFormatter fmt = DateTimeFormat.forPattern("MM/dd.YYYY");
            String str = fmt.print(dt);
        

        【讨论】:

          【解决方案5】:

          试试这个:

          SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
          Date d = sdf.parse("dd/MM.YYYY");
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-05-19
            • 1970-01-01
            • 2016-09-29
            • 2016-09-03
            相关资源
            最近更新 更多