代码之家  ›  专栏  ›  技术社区  ›  Rajesh

如何将日期转换为字符串MM/dd/yyyy格式?[已关闭]

  •  -1
  • Rajesh  · 技术社区  · 11 年前

    我有日期2014-10-01 00:00:00.0 我必须转换为2014年1月10日 我怎么能?

    5 回复  |  直到 11 年前
        1
  •  6
  •   Nurdin    11 年前

    这个怎么样

    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
  •  2
  •   Paresh Mayani jeet    11 年前

    使用SimpleDateFormat:

    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
    String date = sdf.format("Your date");
    
        3
  •  0
  •   Ravi Godara    11 年前

    试试这个

    // 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
  •  0
  •   mtbomb    11 年前

    如果您使用JodaTime库,这非常简单。

        DateTime dt = new DateTime();
        DateTimeFormatter fmt = DateTimeFormat.forPattern("MM/dd.YYYY");
        String str = fmt.print(dt);
    
        5
  •  0
  •   Salah    11 年前

    试试看:

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