Skip to content

Commit 2e8a9a5

Browse files
author
marunjar
committed
improve default time formatting
uses DateFormat for formatting day and hour format time according to locale or system settings: If 24h format is enabled then use format "HH:mm" else use format "hh a" like before.
1 parent 818c559 commit 2e8a9a5

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

library/src/main/java/com/alamkanak/weekview/WeekView.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import android.text.StaticLayout;
1717
import android.text.TextPaint;
1818
import android.text.TextUtils;
19+
import android.text.format.DateFormat;
1920
import android.text.style.StyleSpan;
2021
import android.util.AttributeSet;
2122
import android.util.TypedValue;
@@ -34,6 +35,7 @@
3435
import java.util.Collections;
3536
import java.util.Comparator;
3637
import java.util.List;
38+
import java.util.Locale;
3739

3840
/**
3941
* Created by Raquib-ul-Alam Kanak on 7/21/2014.
@@ -1115,25 +1117,28 @@ public DateTimeInterpreter getDateTimeInterpreter() {
11151117
mDateTimeInterpreter = new DateTimeInterpreter() {
11161118
@Override
11171119
public String interpretDate(Calendar date) {
1118-
SimpleDateFormat sdf;
1119-
sdf = mDayNameLength == LENGTH_SHORT ? new SimpleDateFormat("EEEEE") : new SimpleDateFormat("EEE");
1120-
try{
1121-
String dayName = sdf.format(date.getTime()).toUpperCase();
1122-
return String.format("%s %d/%02d", dayName, date.get(Calendar.MONTH) + 1, date.get(Calendar.DAY_OF_MONTH));
1123-
}catch (Exception e){
1120+
try {
1121+
SimpleDateFormat sdf = mDayNameLength == LENGTH_SHORT ? new SimpleDateFormat("EEEEE M/dd", Locale.getDefault()) : new SimpleDateFormat("EEE M/dd", Locale.getDefault());
1122+
return sdf.format(date.getTime()).toUpperCase();
1123+
} catch (Exception e) {
11241124
e.printStackTrace();
11251125
return "";
11261126
}
11271127
}
11281128

11291129
@Override
11301130
public String interpretTime(int hour) {
1131-
String amPm;
1132-
if (hour >= 0 && hour < 12) amPm = "AM";
1133-
else amPm = "PM";
1134-
if (hour == 0) hour = 12;
1135-
if (hour > 12) hour -= 12;
1136-
return String.format("%02d %s", hour, amPm);
1131+
Calendar calendar = Calendar.getInstance();
1132+
calendar.set(Calendar.HOUR_OF_DAY, hour);
1133+
calendar.set(Calendar.MINUTE, 0);
1134+
1135+
try {
1136+
SimpleDateFormat sdf = DateFormat.is24HourFormat(getContext()) ? new SimpleDateFormat("HH:mm", Locale.getDefault()) : new SimpleDateFormat("hh a", Locale.getDefault());
1137+
return sdf.format(calendar.getTime());
1138+
} catch (Exception e) {
1139+
e.printStackTrace();
1140+
return "";
1141+
}
11371142
}
11381143
};
11391144
}

0 commit comments

Comments
 (0)