如何将用户选择的MM-dd-yyyy格式的日期转换为Java中的yyyy-MM-dd格式

如何将用户选择的MM-dd-yyyy格式的日期转换为Java中的yyyy-MM-dd格式,java,date,simpledateformat,date-formatting,Java,Date,Simpledateformat,Date Formatting,我想将输入日期从MM dd yyyy转换为前端的格式,并将其转换为yyyy-MM-dd格式。这很有用,因为后端查询要求格式为yyyy-MM-dd。语法有问题。我知道当我执行Date=new Date()时,它只针对今天的日期进行初始化,但我希望参数是用户选择的传入日期,可以是任何它想要的日期。我设置了两种日期格式: 我可能设置正确,也可能设置不正确,我不确定是否需要设置两种这样的日期格式。任何帮助都将不胜感激。谢谢 您应该查看SimpleDateFormat 一个简单的例子是 public f

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


我可能设置正确,也可能设置不正确,我不确定是否需要设置两种这样的日期格式。任何帮助都将不胜感激。谢谢

您应该查看
SimpleDateFormat

一个简单的例子是

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

您应该查看
SimpleDateFormat

一个简单的例子是

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

您说过,您希望转换具有给定格式的日期,但没有相应的参数

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

您说过,您希望转换具有给定格式的日期,但没有相应的参数

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);
}
您需要使用
日期格式实际解析输入
字符串

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只需简单地转换为
java.sql.Date
就可以接受
Date
,您可以在以后将其转换为所需的任何格式

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

您需要使用
日期格式实际解析输入
字符串

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只需简单地转换为
java.sql.Date
就可以接受
Date
,您可以在以后将其转换为所需的任何格式


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

这应该可以做到:

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

这应该可以做到:

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);
}
tl;博士 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 该框架内置于Java8及更高版本中。这些类取代了麻烦的旧日期时间类,例如,&

该项目现已启动,建议迁移到类

要了解更多信息,请参阅。并搜索堆栈溢出以获得许多示例和解释。规格是

从哪里获得java.time类

  • ,及以后
    • 内置的
    • 标准JavaAPI的一部分,带有捆绑实现
    • Java9添加了一些次要功能和修复
    • 大部分java.time功能都在中向后移植到Java6和Java7
    • 更高版本的Android捆绑包实现了java.time类
    • 对于早期的Android,该项目采用了ThreeTen Backport(如上所述)。看
该项目使用其他类扩展了java.time。这个项目是java.time将来可能添加的一个试验场。您可以在这里找到一些有用的类,如、、和。

tl;博士 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 该框架内置于Java8及更高版本中。这些类取代了麻烦的旧日期时间类,例如,&

该项目现已启动,建议迁移到类

要了解更多信息,请参阅。并搜索堆栈溢出以获得许多示例和解释。规格是

从哪里获得java.time类

  • ,及以后
    • 内置的
    • 标准JavaAPI的一部分,带有捆绑实现
    • Java9添加了一些次要功能和修复
    • 大部分java.time功能都在中向后移植到Java6和Java7
    • 更高版本的Android捆绑包实现了java.time类
    • 对于早期的Android,该项目采用了ThreeTen Backport(如上所述)。看

该项目使用其他类扩展了java.time。这个项目是java.time将来可能添加的一个试验场。您可以在这里找到一些有用的类,如、、和。

我看不到用户选择的日期。它是如何进入这个函数的呢?仅供参考,像、
java.text.SimpleTextFormat
这样麻烦的旧日期时间类现在被这些类取代了。请参阅。我看不到用户选择的日期。它是如何进入这个函数的呢?仅供参考,像、
java.text.SimpleTextFormat
这样麻烦的旧日期时间类现在被这些类取代了。看,我可以看出我需要格式化传入日期,但是该日期不能只是手动输入。08-10-1999或只是一个要加入的东西我不是在寻找什么,我希望参数是用户可以选择的任何东西,可以是任何东西。让我为您更改它。我可以看到我需要格式化传入日期,但该日期不能只是手动输入。08-10-1999或只是一些扔在那里不是我要找的,我想要的参数只是任何一个用户w