简体   繁体   中英

How to convert a date a user selects in MM-dd-yyyy format to the format yyyy-MM-dd in Java

I want to convert the incoming date from MM-dd-yyyy which is the format on the front end, and convert it to the format yyyy-MM-dd. This is useful since the backend query requires the format to be yyyy-MM-dd. I am having trouble with the syntax. I know when I do Date = new Date(), that only initializes for today's date but I want the paramater to be the incoming date that the user selects, which can be whatever it wants to be. I have two date formats set up:

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
}

I may or may not be setting it up properly and am unsure if I need to set up two DateFormats like that. Any help would be much appreciated. Thanks!

You said, you want to convert a date with a given format, but you don't have a parameter for that.

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

You should look at the format function for SimpleDateFormat

A quick example would be

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

This should do the trick:

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

You need to actually parse the input String using your DateFormat :

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

Output:

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

Note, you're probably better off using the Date object in your code. JDBC can accept the Date with only a simple conversion to a java.sql.Date and you can convert it to any format your need later.

I would also promote the formats for class members as they are expensive to construct so should be reused, although it should be noted that they are not thread safe.

tl;dr

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

2017-01-23

Using java.time

You are using troublesome old date-time classes now supplanted by java.time classes.

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

Your desired output happens to be standard ISO 8601. The java.time classes use the standard formats by default when parsing/generating strings.

String output = ld.toString() ;

2017-01-23


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date , Calendar , & SimpleDateFormat .

The Joda-Time project, now in maintenance mode , advises migration to the java.time classes.

To learn more, see the Oracle Tutorial . And search Stack Overflow for many examples and explanations. Specification is JSR 310 .

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval , YearWeek , YearQuarter , and more .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM