How do I convert String date to Date format date in dd-MM-yyyy format in java

Ravi

I have date in string format as Wed Jun 07 00:00:00 IST 2023 but I want it as in dd-mm-yyyy format in DATE type only... I can convert it but it is converting in String type I want it should convert it into ONLY Date type in dd-mm-yyyy format.

I tried below code but it is converting in String format as Wed Jun 07 00:00:00 IST 2023 but I want output should be something like

Date date = 07-06-2023 only.

Please help me.

My code snippet is:

public static void main(String[] args) throws ParseException {
String dateOfBirht= "07-06-2023";
Date date = new SimpleDateFormat("dd-MM-yyyy").parse(dateOfBirht);
System.out.println(date);

If you see the output of the above code snippet is printing as Wed Jun 07 00:00:00 IST 2023 which is not my desired answer. It should be 07-06-2023 in DATE FORMAT ONLY neither String nor LocaleDate

Reilas

"... I want it as in dd-mm-yyyy format in DATE type only. ..."

I'm not sure I understand the question, entirely.  You already have the Date value, here.

Date date = new SimpleDateFormat("dd-MM-yyyy").parse(dateOfBirht);

"... If you see the output of the above code snippet is printing as Wed Jun 07 00:00:00 IST 2023 which is not my desired answer. It should be 07-06-2023 ..."

I believe you're implying that the Date object should not be returning the time, since you had only parsed the date values.

This isn't the case.  Similarly, it is still a valid date; the zeroed time fields should play no role in the date fields.

Nevertheless, you can use a basic String format.
Format specifiers can be found on the Formatter class JavaDoc.

System.out.printf("%td-%1$tm-%1$tY", date);

Output

07-06-2023

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to convert string date (25.04.2016) to date format (mm/dd/yyyy)?

How do I convert "dd.mm.yyyy" string to date format in golang?

how to format String to Date with format dd-mm-yyyy in java

How to convert Date to String into the format yyyy-mm-dd in typescript

What date format is this and how do I convert it into a MM-DD-YYYY format and vice versa

How do I get a date in YYYY-MM-DD format?

how can I convert date(YYYY, MM, DD) format to YYYY/MM/DD with python?

Convert date format into MM/DD/YYYY

Convert dd/mm/yyyy H to date format

How can I convert date format from YYYY-MM-DD to YYYY-MM in sql

Convert Crystal Reports string to Date format yyyy-MM-dd

Convert string of YYYYMMDD to YYYY-MM-DD Date format in Snowflake

Convert string of date to datetime object in format YYYY-MM_DD

How to convert a string in yyyy-mm-dd format to a Javascript Date Object without considering the date as UTC

How to convert string date to following date format dd/MM/yyyy t:m:s in C#

How to convert DateBox's date string into Date_SHORT("YYYY-MM-DD") format?

How to convert Date format to dd-MM-yyyy in Java which return a Date object

How to convert mmddyyyy date format to mm/dd/yyyy in nodejs

how to convert long date value to mm/dd/yyyy format

How to convert date in to yyyy-MM-dd Format?

How to convert date into this 'yyyy-MM-dd' format in angular 2

How to convert ISO date format into 'MM/DD/YYYY' in bootstrap/angular

How to convert ISOString to date format dd/mm/yyyy using javascript

How to convert "YYYY-MM-DD" format to Date object

How to convert MMMM yyyy format of date in yyyy-MM-DD Date format

Convert date in YYYY-MM-DD format to YYYY-MM

How to format date in MM/DD/YYYY in Delphi

django how to format date to DD/MM/YYYY

How do I display a date format in dd/MM/yyyy to ddd, dd/MM/yyyy (Mon, 1/4/2014) format with this logic?