-3

I want to convert a date that I receive as a parameter as follows:

String fecha = "yyyymmdd"; ----- Received as a parameter.

convert it to "yymmdd", i did it with SimpleDateFormat, but it doesn't work for me, it tells me that the conversion is not correct.

I am doing it in java

I attach code of how I am doing it in java

public String findFile(String date) {
    SimpleDateFormat sdf = SimpleDateFormat("yymmdd");
    String fecha = sdf.format(date);
    System.out.println("Formato de fecha: " fecha);
}
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • 1
    Please post this question here: https://es.stackoverflow.com/ Pregunta en la version de Espanol aqui: https://es.stackoverflow.com/ – Marcelo Paco Mar 27 '23 at 20:49
  • 1
    `#format` expects a `Date`, you can use two separate formatters (one to `#parse` the string, another to `#format` the resulting `Date`). – Rogue Mar 27 '23 at 20:58
  • Yes, but the date is received as String – Jonathan Ortiz Mar 27 '23 at 21:05
  • 2
    1. Use the new `java.time` API, including `DateTimeFormatter` and `LocalDate`; 2. The input formatter MUST match the input text (ie `yyyyMMdd`); 3. The formatting of date/time values should really only be consider when display information to the user, where possible, APIs and storage should be using standardised date/time formats – MadProgrammer Mar 27 '23 at 21:08
  • 2
    `LocalDate.parse("20000611", DateTimeFormatter.ofPattern("yyyyMMdd")).format(DateTimeFormatter.ofPattern("yyMMdd"))` – MadProgrammer Mar 27 '23 at 21:10
  • Which error messages are you getting? In my IntelliJ IDEA I get *Method call expected* at `SimpleDateFormat("yymmdd")` in the first line inside the method. That will go away when you change to using `DateTimeFormatter` as shown by @MadProgrammer. – Ole V.V. Mar 27 '23 at 21:51
  • 1
    To those who think that this should be re-opened, I just want to point out that; 1. The original question should have been deleted and new, English question posted and 2. It would most likely be closed as a duplicate against any of the hundreds, if not thousands, of other simular questions asked on SO anyway - this is not a new or unique question – MadProgrammer Mar 27 '23 at 21:53
  • For the vast majority of purposes you should not wish to change a date in a string in one format to a string in a different format. In your program keep the date as a `LocalDate` throughout. When you receive string input, parse into a `LocalDate` first thing. Only when you need to give string output, format back into a string. – Ole V.V. Mar 27 '23 at 21:54
  • 1
    @MadProgrammer is correct: this can be seen as a duplicate of for example [this question](https://stackoverflow.com/questions/4772425/change-date-format-in-a-java-string), [this one](https://stackoverflow.com/questions/16418193/java-method-call-expected) and/or [this one](https://stackoverflow.com/questions/10649782/java-cannot-format-given-object-as-a-date). (Could also have closed as needing debugging details, but that would have been less friendly and less helpful.) – Ole V.V. Mar 27 '23 at 21:59
  • @programador loco, es correcto lo que pones, solo que así como lo pones obtienes la actual y yo quiere recibir cualquier fecha, por ejemplo si tengo una fecha de ayer, pues quiero recibir ese fecha de ayer – Jonathan Ortiz Mar 27 '23 at 22:38
  • Re-opened after being rewritten in English. – Basil Bourque Mar 28 '23 at 00:32
  • I changed the title to be easier to find for future duplicates. I am sure we have earlier originals, but finding one has eluded me. – Basil Bourque Mar 28 '23 at 00:34

1 Answers1

2

tl;dr

LocalDate
.parse( "20230123" , DateTimeFormatter. BASIC_ISO_DATE )
.format(
    DateTimeFormatter.ofPattern( "uuMMdd" )
)

Details

Parse your input as a LocalDate.

Your input text complies with the “basic” version of the standard ISO 8601 format for a date value. Java comes bundled with a predefined formatter, BASIC_ISO_DATE.

String input = "20230129" ;
LocalDate ld = LocalDate.parse( input , DateTimeFormatter. BASIC_ISO_DATE ) ;

To generate text in other formats, define your desired formatting pattern. Follow the rules laid out in DateTimeFormatter Javadoc. Notice how uppercase/lowercase matters.

DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuMMdd" ) ;
String output = ld.format( f ) ;

230129

I suggest you reconsider omitting the century. This creates a very confusing piece of text, easy for humans to misread. Stick with expanded ISO 8601 format, with hyphens as delimiters: YYYY-MM-DD.

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