seher kaya
seher kaya

Reputation: 35

How to convert Time format to today's time in Date format in java?

I have a value of "00.00.00" in Time format and I want to convert it to a value in Date format, such as "2019-09-02 00.00.00".

Upvotes: 2

Views: 931

Answers (3)

Joakim Danielson
Joakim Danielson

Reputation: 51945

If the end result you want is a string then you can format the date to a string and append them

String str = "00.00.00";
LocalDate date = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE;

 String dateTime = date.format(formatter) + " " + str;

or in a more compact format

DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE;
String dateTime = String.format("%s %s", LocalDate.now().format(formatter), str);

If on the other hand the time is always "00.00.00" you can include it in your formatter pattern

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd 00.00.00");
LocalDate date = LocalDate.now();
String dateTime = date.format(formatter)

Upvotes: 1

Youcef LAIDANI
Youcef LAIDANI

Reputation: 59978

You can use

DateTimeFormatter format = DateTimeFormatter.ofPattern("HH.mm.ss"); 
LocalDateTime dt = LocalDateTime.of(LocalDate.now(), LocalTime.parse(yourString, format));

Your result is not default of LocalDateTime so to get your result just use another formatter :

 DateTimeFormatter format2 = DateTimeFormatter.ofPattern("uuuu-MM-dd HH.mm.ss");
 String result = dt.format(format2);

Upvotes: 3

Ng Sharma
Ng Sharma

Reputation: 2192

LocalDateTime ldt = LocalDateTime.now();

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd hh.mm.ss");
String newDateTimeFormatter = ldt.format(dtf);

if you try this.

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd '00.00.00'");
String newDateTimeFormatter = ldt.format(dtf);

Upvotes: 0

Related Questions