The answer by YCF_L is correct and to-the-point. The reason why I have written this answer is I have seen similar kind of questions (why my date/time is not being printed in a custom way) being asked every now and then.
Note that LocalDate
, LocalTime
, LocalDateTime
etc. each have their own toString()
implementation and no matter what you do, whenever you print their object, their toString()
method will be called and thus always their default representation will be printed. If you want these objects to be printed in a custom way, you have two options:
- You get their elements (e.g. year, month and day from an object of
LocalDate
) and print them by arranging in your custom way.
- Use a formatter class e.g. (the modern
DateTimeFormatter
or the legacy SimpleDateFormat
) and get a string representing the date/time object in a custom way.
To make your code reusable and clean, you prefer the second approach.
The following example illustrates the same:
class Name {
private String firstName;
private String lastName;
public Name() {
firstName = "";
lastName = "";
}
public Name(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
@Override
public String toString() {
return firstName + " " + lastName;
}
}
class NameFormatter {
// Returns a name (e.g. First Last) as F. Last
public static String patternIntialsLast(Name name) {
if (name.getFirstName().length() > 1) {
return name.getFirstName().charAt(0) + ". " + name.getLastName();
}
return name.toString();
}
// Returns a name (e.g. First Last) as Last F.
public static String patternLastInitials(Name name) {
if (name.getFirstName().length() > 1) {
return name.getLastName() + " " + name.getFirstName().charAt(0) + ".";
}
return name.toString();
}
// Returns a name (e.g. First Last) as Last First
public static String patternLastIFirst(Name name) {
return name.getLastName() + ", " + name.getFirstName();
}
}
public class Main {
public static void main(String[] args) {
Name name = new Name("Foo", "Bar");
System.out.println("Default format:");
System.out.println(name);// It will always print what name.toString() returns
// If you want to print name in different formats use NameFormatter e.g.
System.out.println("\nIn custom formats:");
String strName1 = NameFormatter.patternIntialsLast(name);
System.out.println(strName1);
String strName2 = NameFormatter.patternLastIFirst(name);
System.out.println(strName2);
String strName3 = NameFormatter.patternLastInitials(name);
System.out.println(strName3);
}
}
Output:
Default format:
Foo Bar
In custom formats:
F. Bar
Bar, Foo
Bar F.
Now, go through the answer by YCF_L again and this time, you know that you have to implement your method as follows:
public String getTime() {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");
return formatter.format(ldt);
}
A quick demo:
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
public class Main {
// Now
static LocalDateTime ldt = LocalDateTime.of(LocalDate.now(), LocalTime.now());
public static void main(String[] args) {
System.out.println(getTime());
}
public static String getTime() {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");
return formatter.format(ldt);
}
}
Output:
22:23