【问题标题】:How to convert a date a user selects in MM-dd-yyyy format to the format yyyy-MM-dd in Java如何将用户以 MM-dd-yyyy 格式选择的日期转换为 Java 中的 yyyy-MM-dd 格式
【发布时间】:2014-03-18 14:37:22
【问题描述】:

我想将前端格式MM-dd-yyyy的传入日期转换为yyyy-MM-dd格式。这很有用,因为后端查询要求格式为 yyyy-MM-dd。我在语法上有问题。我知道当我执行 Date = new Date() 时,它只为今天的日期初始化,但我希望参数是用户选择的传入日期,可以是任何它想要的日期。我设置了两种日期格式:

public final static String getConvertedDate()
{
    DateFormat userFormat = new SimpleDateFormat(MM-dd-yyyy);
    DateFormat neededFormat = new SimpleDateFormat(yyyy-MM-dd);
    //now I want to set it up to where I have a date the user selects
    Date date = new Date() //I feel like I should input a parameter for any given date in Date() but am    //unsure
    //return statement returning the neededFormat
}

我可能会或可能不会正确设置它,并且不确定是否需要设置两个这样的 DateFormats。任何帮助将非常感激。谢谢!

【问题讨论】:

标签: java date simpledateformat date-formatting


【解决方案1】:

你说,你想用给定格式转换日期,但你没有参数。

public final static String getConvertedDate(String givenDaten) throws ParseException
{
    DateFormat userFormat = new SimpleDateFormat(MM-dd-yyyy);
    DateFormat neededFormat = new SimpleDateFormat(yyyy-MM-dd);

    //first parse the userformatted date
    Date userFormatDate = userFormat.parse(givenDate);

    //format it as needed
    return neededFormat.format(date);
}

【讨论】:

    【解决方案2】:

    这应该可以解决问题:

    public String convertDate(String input) throws ParseException{
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");
        Date temp = sdf.parse(input);
        sdf.applyPattern("yyyy-MM-dd");
        return sdf.format(temp);
    }
    

    【讨论】:

      【解决方案3】:

      您应该查看SimpleDateFormatformat 函数

      一个简单的例子是

      public final static String getConvertedDate(String userDate)
      {
          DateFormat userFormat = new SimpleDateFormat(MM-dd-yyyy);
          DateFormat neededFormat = new SimpleDateFormat(yyyy-MM-dd);
      
          // Turn the String of the userDate into a date with the first format
          Date formatUserDate = userFormat.parse(userDate);
      
          // Now format that date into the correct format you want to return
          return neededFormat.format(formatUserDate);
      }
      

      【讨论】:

      • 我可以看到我需要格式化传入日期,但是该日期不能只是手动输入。 1999 年 8 月 10 日或只是扔进去的东西不是我想要的,我希望参数是用户选择的任何东西,可以是任何东西。
      • 让我给你改一下
      【解决方案4】:

      您需要使用您的DateFormat 实际解析输入String

      public static void main(String[] args) throws Exception {
          final DateFormat in = new SimpleDateFormat("MM-dd-yyyy");
          final Date date = in.parse("03-11-2013");
          System.out.println(date);
          final DateFormat out = new SimpleDateFormat("yyyy-MM-dd");
          System.out.println(out.format(date));
      }
      

      输出:

      Mon Mar 11 00:00:00 CET 2013
      2013-03-11
      

      注意,您最好在代码中使用Date 对象。 JDBC 可以接受Date,只需简单地转换为java.sql.Date,您可以稍后将其转换为您需要的任何格式。

      我也会推广类成员的格式,因为它们的构造成本很高,因此应该重用,尽管应该注意它们不是线程安全的。

      【讨论】:

        【解决方案5】:

        tl;博士

        LocalDate.parse( 
            "01-23-2017" , 
            DateTimeFormatter.ofPattern( "MM-dd-uuuu" ) 
        ).toString()
        

        2017-01-23

        使用 java.time

        您正在使用麻烦的旧日期时间类,现在已被 java.time 类取代。

        String input = "01-23-2017" ;
        DateTimeFormatter f = DateTimeFormatter.ofPattern( "MM-dd-uuuu" ) ;
        LocalDate ld = LocalDate.parse( input , f );
        

        您想要的输出恰好是标准 ISO 8601。java.time 类在解析/生成字符串时默认使用标准格式。

        String output = ld.toString() ;
        

        2017-01-23


        关于java.time

        java.time 框架内置于 Java 8 及更高版本中。这些类取代了麻烦的旧 legacy 日期时间类,例如 java.util.DateCalendarSimpleDateFormat

        Joda-Time 项目现在位于maintenance mode,建议迁移到java.time 类。

        要了解更多信息,请参阅Oracle Tutorial。并在 Stack Overflow 上搜索许多示例和解释。规格为JSR 310

        从哪里获得 java.time 类?

        ThreeTen-Extra 项目通过附加类扩展了 java.time。该项目是未来可能添加到 java.time 的试验场。您可以在这里找到一些有用的类,例如IntervalYearWeekYearQuartermore

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-06-24
          • 1970-01-01
          • 2016-01-02
          • 1970-01-01
          • 1970-01-01
          • 2023-03-15
          • 1970-01-01
          相关资源
          最近更新 更多