-1

I need some support. I desired convert a variable String to Date. The variable Date should be format dd-MM-yyyy.

 import java.util.Date;
    ....
    ...
    String a = "2022-05-12";
    Date b; // should be dd-MM-yyyy 
    
    do some to format...
    
    return b; // return b with format dd-MM-yyyy, remember this variable is type Date no String

I was trying to do something but the format obtained is not the desired one.

enter image description here

zannier
  • 21
  • 1
  • 10
  • When you want to print a `Date`, you need to format it again. You can have a look at the implementation of the `toString` method. If your are only interested in the days, [LocalDate](https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html) might be a good pick. – thinkgruen May 13 '22 at 22:51
  • 4
    a `Date` has only a one format when using its `toString` method - that format cannot be changed - a formatter must be used to `format` it into a string. [Please do not upload images of code/data/errors when asking a question.](//meta.stackoverflow.com/q/285551) – user16320675 May 13 '22 at 22:55
  • 2
    `Date` represents an instant in time, not a date. It's very unfortunately named. You should be using `java.time.LocalDate`, or `java.time.ZonedDateTime`, or similar. None of those represent a format. To 'print' such an object with a specific format, pass it to a formatter which gives you a string. – rzwitserloot May 13 '22 at 22:59
  • 4
    1. Don't use `SimpleDateFormat` or `Date`, they are out-of-date, use the `java.time.*` APIs instead; 2. Dates (in general) are simply containers for the amount of time which has passed since a given point in time (ie the Unix epoch), they don't, by design, have a concept of "format", the `toString` implementation is simply there to provide information, this is what formatters are used for, you format the date/time object to a `String` – MadProgrammer May 13 '22 at 23:00

2 Answers2

5

tl;dr

LocalDate
.parse( "2022-05-12" )
.format(
    DateTimeFormatter.ofPattern( "dd-MM-uuuu" )
)

12-05-2022

java.time

Use modern java.time classes. Never use the terrible Date, Calendar, SimpleDateFormat classes.

ISO 8601

Your input conforms to ISO 8601 standard format used by default in the java.time classes for parsing/generating text. So no need to specify a formatting pattern.

LocalDate

Parse your date-only input as a date-only object, a LocalDate.

String input = "2022-05-12" ;
LocalDate ld = LocalDate.parse( input ) ;

To generate text in your desired format, specify a formatting pattern.

DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd-MM-uuuu" ) ;
String output = ld.format( f ) ;

Rather than hardcode a particular pattern, I suggest learning to automatically localize using DateTimeFormatter.ofLocalizedDate.

All this has been covered many many times already on Stack Overflow. Always search thoroughly before posting. Search to learn more.

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

What you are doing is converting from string to date. It seems that you want it to PRINT the date in a specific format.

Where is an example of how to do it:

DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
System.out.println(dateFormat.format(date)); //2022/05/18 00:18:43
Rui Menoita
  • 1
  • 1
  • 2
  • 1
    You are using terrible date-time classes that were years ago supplanted by the modern *java.time* classes defined in JSR 310. – Basil Bourque May 14 '22 at 02:25
  • I mean I gave an example of the stuff he is working with. You don't really know which jdk he is using. – Rui Menoita May 15 '22 at 15:35
  • The *java.time* classes are built into every version of Java not past End-Of-Life, Java 8 through Java 18. For those stuck on Java 6 or 7, most of the *java.time* functionality is available in the *ThreeTen-Backport* library. There really is no need to be using those terrible legacy classes. – Basil Bourque May 15 '22 at 15:41