[英]JodaTime: How to get a String representation in the format “HH:mm Z” from a LocalTime
[英]How to properly format LocalTime to HH:MM?
我正在嘗試使用吸氣劑來獲取HH:mm
中的 LocalTime ,只要它被調用。 就目前而言,它是:
private LocalTime time;
public LocalTime getTime() {
return time;
}
我希望它以HH:mm
返回時間,因為它現在是HH:mm:SS.s
。 我試圖弄亂日期時間格式化程序,但我無法弄清楚。 這是我所擁有的:
private LocalTime time;
public LocalTime getTime() {
DateTimeFormatter formatDateTime = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
LocalTime localFormattedTime = LocalTime.parse(time, formatDateTime);
return localFormattedTime;
}
YCF_L 的回答是正確且中肯的。 我寫這個答案的原因是我經常看到類似的問題(為什么我的日期/時間沒有以自定義方式打印)。
請注意, LocalDate
、 LocalTime
、 LocalDateTime
等都有自己的toString()
實現,無論您做什么,無論何時打印它們的 object,都會調用它們的toString()
方法,因此總是會打印它們的默認表示。 如果您希望以自定義方式打印這些對象,您有兩種選擇:
LocalDate
的 object 中獲取它們的元素(例如年、月和日)並通過以您的自定義方式排列來打印它們。DateTimeFormatter
或傳統SimpleDateFormat
)並以自定義方式獲取表示日期/時間 object 的字符串。為了使您的代碼可重用和干凈,您更喜歡第二種方法。
以下示例說明了相同的情況:
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.
現在,go 再次通過 YCF_L 的回答,這一次,你知道你必須實現你的方法如下:
public String getTime() {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");
return formatter.format(ldt);
}
快速演示:
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
LocalTime
沒有日期部分它只有時間部分LocalDate
, LocalTime
, LocalDateTime
不能有特定的格式,它使用標准格式,你不能改變它。LocalDate
, LocalTime
, LocalDateTime
。嘗試這樣的事情:
String localTimeString = "23:59:59";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm:ss");
try {
TemporalAccessor ta = dtf.parse(localTimeString);
LocalTime lt = LocalTime.from(ta);
LOGGER.info("lt: {}", lt);
} catch (RuntimeException re) {
LOGGER.error("Unable to parse local time string: [{}]", localTimeString, re);
}
有關它支持的模式的詳細信息,請參閱DateTimeFormatter上的 Java 文檔。
您還可以使用 DateTimeFormatter 將 LocalTime 格式化回字符串形式,如下所示:
String localTimeAsString = dtf.format(lt)
LOGGER.info("LocalTime as string: {}", localTimeAsString);
試試這個。 ID
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH.mm"); LocalTime today = LocalTime.now(); String timeString = today.format(formatter); //12.38
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.