1

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!

anirudh
  • 4,116
  • 2
  • 20
  • 35
wheelerlc64
  • 435
  • 2
  • 5
  • 17
  • I don't see the user-selected date. How is it getting into this function? – Alex Wittig Mar 18 '14 at 14:44
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html), and `java.text.SimpleTextFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [java.time](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) classes. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Jun 18 '17 at 01:15

5 Answers5

3

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);
}
Zhedar
  • 3,480
  • 1
  • 21
  • 44
1

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);
}
  • I can see that I need to format the incoming date, however that date can't just be a manual input. 08-10-1999 or just something to throw in there is not what I'm looking for, I want the parameter to be just whatever a user would select, which could be anything. – wheelerlc64 Mar 18 '14 at 14:47
  • Let me change it for you – Stephen Corcoran Mar 18 '14 at 14:49
1

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);
}
cosmin.danisor
  • 943
  • 1
  • 12
  • 29
0

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.

Boris the Spider
  • 59,842
  • 6
  • 106
  • 166
0

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.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154