날짜 형식화 후 오전 및 오후를 소문자로 표시
datetime 형식을 지정하면 시간은 AM 또는 PM을 대문자로 표시하지만 am 또는 pm과 같이 소문자로 표시하고 싶습니다.
이것은 내 코드입니다.
public class Timeis {
public static void main(String s[]) {
long ts = 1022895271767L;
String st = null;
st = new SimpleDateFormat(" MMM d 'at' hh:mm a").format(ts);
System.out.println("time is " + ts);
}
}
불행히도 표준 서식 지정 방법으로는 그렇게 할 수 없습니다. Joda도 마찬가지입니다. 형식이 지정된 날짜를 간단한 형식 후 교체로 처리해야한다고 생각합니다.
String str = oldstr.replace("AM", "am").replace("PM","pm");
replaceAll()
regepxs를 사용하는 방법을 사용할 수 있지만 위의 내용으로 충분하다고 생각합니다. 나중에 형식 문자열을 변경하여 월 이름 또는 이와 유사한 이름을 포함하도록 변경하면 형식이 잘못 될 수 있으므로 담요를 사용 하지 않습니다toLowerCase()
.
편집 : James Jithin의 솔루션 이 훨씬 좋아 보이며이를 수행하는 적절한 방법 (댓글에 언급 됨)
이것은 작동합니다
public class Timeis {
public static void main(String s[]) {
long ts = 1022895271767L;
SimpleDateFormat sdf = new SimpleDateFormat(" MMM d 'at' hh:mm a");
// CREATE DateFormatSymbols WITH ALL SYMBOLS FROM (DEFAULT) Locale
DateFormatSymbols symbols = new DateFormatSymbols(Locale.getDefault());
// OVERRIDE SOME symbols WHILE RETAINING OTHERS
symbols.setAmPmStrings(new String[] { "am", "pm" });
sdf.setDateFormatSymbols(symbols);
String st = sdf.format(ts);
System.out.println("time is " + st);
}
}
이 시도:
System.out.println("time is " + ts.toLowerCase());
여기 와 여기에 자세히 설명 된대로 사용자 지정 형식을 만들 수 있지만
불행하게도 밖으로 상자 에 AM과 PM은 표준 SimpleDateFormat의 클래스 정의로하지 않는 것
문자열 대체를 원하지 않고 Java 8을 사용하는 경우 javax.time
:
Map<Long, String> ampm = new HashMap<>();
ampm.put(0l, "am");
ampm.put(1l, "pm");
DateTimeFormatter dtf = new DateTimeFormatterBuilder()
.appendPattern("E M/d h:mm")
.appendText(ChronoField.AMPM_OF_DAY, ampm)
.toFormatter()
.withZone(ZoneId.of("America/Los_Angeles"));
DateTimeFormatter
소문자 am / pm에 대한 패턴 기호가 없기 때문에 (개별 조각 지정) 수동으로 빌드해야합니다 . appendPattern
전후에 사용할 수 있습니다 .
기본 am / pm 기호를 대체 할 수있는 방법이 없다고 생각합니다. 이것이 최종 문자열에서 문자열 바꾸기를 수행하는 유일한 방법입니다.
Calendar c = Calendar.getInstance();
System.out.println("Current time => " + c.getTime());
SimpleDateFormat df = new SimpleDateFormat("HH:mm a");
String formattedDate = df.format(c.getTime());
formattedDate = formattedDate.replace("a.m.", "AM").replace("p.m.","PM");
TextView textView = findViewById(R.id.textView);
textView.setText(formattedDate);
James's answer is great if you want different style other than default am, pm. But I'm afraid you need mapping between Locale and Locale specific AM/PM set to adopting the override. Now you simply use java built-in java.util.Formatter class. So an easy example looks like this:
System.out.println(String.format(Locale.UK, "%1$tl%1$tp", LocalTime.now()));
It gives:
9pm
To note that if you want upper case, just replace "%1$tp" with "%1$Tp". You can find more details at http://docs.oracle.com/javase/8/docs/api/java/util/Formatter.html#dt.
just add toLowarCase()
like this
public class Timeis {
public static void main(String s[]) {
long ts = 1022895271767L;
String st = null;
st = new SimpleDateFormat(" MMM d 'at' hh:mm a").format(ts).toLowerCase();
System.out.println("time is " + ts);
}
}
and toUpperCase()
if you want upper case
String today = now.format(new DateTimeFormatterBuilder()
.appendPattern("MM/dd/yyyy ")
.appendText(ChronoField.AMPM_OF_DAY)
.appendLiteral(" (PST)")
.toFormatter(Locale.UK));
// output => 06/18/2019 am (PST)
Locale.UK => am or pm; Locale.US => AM or PM; try different locale for your needs (defaul, etc.)
'Development Tip' 카테고리의 다른 글
Q_OBJECT에서 'vtable에 대한 정의되지 않은 참조'오류 발생 (0) | 2020.11.29 |
---|---|
! function ($) {$ (function () {})} (window.jQuery)는 무엇을합니까? (0) | 2020.11.29 |
Intellij IDEA : 중단 점이 적중되지 않고 회색으로 표시됨 (0) | 2020.11.29 |
Pandas를 사용하여 두 열 비교 (0) | 2020.11.29 |
homebrew로 node.js 설치 문제 (0) | 2020.11.29 |