Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert String into DateFormat in java?

Tags:

java

date

I am working in Java. I have a String and I want to convert it into Date format. Please see the code below for more details.

String dateString = "4:16:06 PM";

I want to convert it into the below Date, using the current day, month, and year:

Date convertedDate = 2014-04-22 16:16:06.00

How can I do this?

like image 720
user2963481 Avatar asked Jan 24 '26 02:01

user2963481


2 Answers

try this:

// get current date
SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
String a = dateFormat1.format(new Date());

// add current date to your time string
String dateString = a + " 4:16:06 PM";

SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd h:mm:ss a");
try {
    // parse it. This is you date object.
    Date d = dateFormat2.parse(dateString);
} catch (ParseException ex) {
    ex.printStackTrace();
}
like image 170
jdiver Avatar answered Jan 25 '26 16:01

jdiver


tl;dr

ZonedDateTime
.of  // ( date , time , zone )
(
    LocalDate.of ( 2014 , Month.APRIL , 22 ) ,
    LocalTime.parse 
    ( 
        "4:16:06 PM" , 
        DateTimeFormatter.ofPattern ( "h:mm:ss a" ).withLocale ( Locale.of ( "en" , "US" ) ) 
    ) ,
    ZoneId.of ( "Africa/Tunis" )
)

Modern Java

In Java 8+, use the java.time classes for all your date-time needs. Never use the terribly flawed legacy classes such as Date, Calendar, SimpleDateFormat, etc.

java.time.LocalTime

Apparently you want to parse text representing a time-of-day.

String dateString = "4:16:06 PM";
Locale locale = Locale.of ( "en" , "US" ); // In earlier Java: Locale.forLanguageTag( "en-US" )
DateTimeFormatter formatter = DateTimeFormatter.ofPattern ( "h:mm:ss a" ).withLocale ( locale );
LocalTime lt = LocalTime.parse (dateString, formatter);

lt.toString() = 16:16:06

Date-time objects are not text

You said:

I want to convert it into Date Format

📌 Crucial point to learn: Date-time objects do not have a “format”. Text has a format. Date-time objects are not text.

Date-time objects can generate text. And you can parse text to instantiate date-time objects. Use a DateTimeFormatter object for both purposes.

java.time.LocalDate

You said:

I want to convert it into … 2014-04-22 16:16:06.00

Apparently you want to combine that time-of-day we parsed above with a particular date. For the date portion, use java.time.LocalDate class.

LocalDate ld = LocalDate.of ( 2014 , Month.APRIL , 22 ) ;

For the current date:

ZoneId z = ZoneId.of ( "Pacific/Auckland" ) ;
LocalDate ld = LocalDate.now ( z ) ;

java.time.LocalDateTime

Combine the date and time into LocalDateTime object.

LocalDateTime ldt = LocalDateTime.of( ld , lt ) ; 

java.time.ZonedDateTime

Be aware that you have not determined a moment, a point on the timeline. We have no idea if you meant 4 PM in Tokyo Japan, 4 PM in Toulouse France, or 4 PM in Toledo Ohio US — three very different moments, several hours apart. To determine a moment, supply the time zone in which you want to interpret the date and the time.

Apply a ZoneId to produce ZonedDateTime object.

ZoneId z = ZoneId.of ( "America/Edmonton" ) ;
ZonedDateTime zdt = ldt.atZone ( z ) ;
like image 37
Basil Bourque Avatar answered Jan 25 '26 15:01

Basil Bourque



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!