사용자 설정에 따라 형식이 지정된 날짜 (년, 월, 날짜) 및 시간 (시, 분, 초) 문자열을 얻으려고합니다. Android 개발자 Google 그룹 의이 게시물은 제가 겪고있는 정확한 문제를 설명하지만 그 사람이 문제를 해결하도록 도와주지 않았습니다. 요약하겠습니다.
Android에는 위의 작업을 시도하는 이러한 클래스가 있지만 사용자 환경 설정과 초를 모두 사용하는 클래스는 없습니다.
java.text.DateFormat
설정 앱에 설정된 기본 설정을 사용하지 않습니다.
android.text.format.DateFormat 및을 사용하여 출력 형식을 올바르게 지정 하지만 초는 포함하지 않는 개체를
가져 옵니다 .java.text.DateFormat
getDateFormat()
getTimeFormat()
getTimeFormat()
android.text.format.DateUtils
설정 앱에서 날짜를 표시하기 위해 설정된 기본 설정을 사용하지 않으며 초를 표시 할 방법도 없습니다.
예를 들어 설정의 기본 설정을 DD / MM / YYYY로 설정하고 24 시간 형식 = on으로 설정하면 다음 코드를 실행합니다.
long timestamp=System.currentTimeMillis();
sometextview.setText(
java.text.format.DateFormat.getDateTimeInstance().format(new Date(timestamp))
+"\n"+
android.text.format.DateFormat.getDateFormat(this).format(new Date(timestamp))
+" "+
android.text.format.DateFormat.getTimeFormat(this).format(new Date(timestamp))
+"\n"+
android.text.format.DateUtils.formatDateTime(this,
timestamp,
DateUtils.FORMAT_SHOW_DATE|
DateUtils.FORMAT_SHOW_TIME|
DateUtils.FORMAT_SHOW_YEAR)
);
textview에서 다음 출력을 제공합니다.
Apr 27,2014 5:47:18 PM
27/04/2014 17:47
April 27,2014, 17:47
None은 원하는 출력을 제공하며 앞서 언급 한 환경 설정을 사용하면 다음과 같습니다.
27/04/2014 17:47:18
joda-time-android 라이브러리도 살펴 봤지만 필요한 작업을 수행하지 않는 것 같습니다 (내가 틀렸다면 수정).
요약 : Android 에서 초 단위 로 사용자 선호도에 따라 날짜 / 시간 형식을 어떻게 지정합니까 ?
사용 @I wish I could think of a good
의 제안을, 나는 형식은 로케일과 사용자 설정을 사용하여 시행 일자에 다음 코드를했다 :
public static String timeDateStringFromTimestamp(Context applicationContext,long timestamp){
String timeDate;
String androidDateTime=android.text.format.DateFormat.getDateFormat(applicationContext).format(new Date(timestamp))+" "+
android.text.format.DateFormat.getTimeFormat(applicationContext).format(new Date(timestamp));
String javaDateTime = DateFormat.getDateTimeInstance().format(new Date(timestamp));
String AmPm="";
if(!Character.isDigit(androidDateTime.charAt(androidDateTime.length()-1))) {
if(androidDateTime.contains(new SimpleDateFormat().getDateFormatSymbols().getAmPmStrings()[Calendar.AM])){
AmPm=" "+new SimpleDateFormat().getDateFormatSymbols().getAmPmStrings()[Calendar.AM];
}else{
AmPm=" "+new SimpleDateFormat().getDateFormatSymbols().getAmPmStrings()[Calendar.PM];
}
androidDateTime=androidDateTime.replace(AmPm, "");
}
if(!Character.isDigit(javaDateTime.charAt(javaDateTime.length()-1))){
javaDateTime=javaDateTime.replace(" "+new SimpleDateFormat().getDateFormatSymbols().getAmPmStrings()[Calendar.AM], "");
javaDateTime=javaDateTime.replace(" "+new SimpleDateFormat().getDateFormatSymbols().getAmPmStrings()[Calendar.PM], "");
}
javaDateTime=javaDateTime.substring(javaDateTime.length()-3);
timeDate=androidDateTime.concat(javaDateTime);
return timeDate.concat(AmPm);
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다