Skip to content

Commit df5a80b

Browse files
author
Jens Claes
committed
Merge pull request #191 from marunjar/default-time-formatter
improve default time formatting
2 parents 0b110e0 + 2e8a9a5 commit df5a80b

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.
@@ -1121,25 +1123,28 @@ public DateTimeInterpreter getDateTimeInterpreter() {
11211123
mDateTimeInterpreter = new DateTimeInterpreter() {
11221124
@Override
11231125
public String interpretDate(Calendar date) {
1124-
SimpleDateFormat sdf;
1125-
sdf = mDayNameLength == LENGTH_SHORT ? new SimpleDateFormat("EEEEE") : new SimpleDateFormat("EEE");
1126-
try{
1127-
String dayName = sdf.format(date.getTime()).toUpperCase();
1128-
return String.format("%s %d/%02d", dayName, date.get(Calendar.MONTH) + 1, date.get(Calendar.DAY_OF_MONTH));
1129-
}catch (Exception e){
1126+
try {
1127+
SimpleDateFormat sdf = mDayNameLength == LENGTH_SHORT ? new SimpleDateFormat("EEEEE M/dd", Locale.getDefault()) : new SimpleDateFormat("EEE M/dd", Locale.getDefault());
1128+
return sdf.format(date.getTime()).toUpperCase();
1129+
} catch (Exception e) {
11301130
e.printStackTrace();
11311131
return "";
11321132
}
11331133
}
11341134

11351135
@Override
11361136
public String interpretTime(int hour) {
1137-
String amPm;
1138-
if (hour >= 0 && hour < 12) amPm = "AM";
1139-
else amPm = "PM";
1140-
if (hour == 0) hour = 12;
1141-
if (hour > 12) hour -= 12;
1142-
return String.format("%02d %s", hour, amPm);
1137+
Calendar calendar = Calendar.getInstance();
1138+
calendar.set(Calendar.HOUR_OF_DAY, hour);
1139+
calendar.set(Calendar.MINUTE, 0);
1140+
1141+
try {
1142+
SimpleDateFormat sdf = DateFormat.is24HourFormat(getContext()) ? new SimpleDateFormat("HH:mm", Locale.getDefault()) : new SimpleDateFormat("hh a", Locale.getDefault());
1143+
return sdf.format(calendar.getTime());
1144+
} catch (Exception e) {
1145+
e.printStackTrace();
1146+
return "";
1147+
}
11431148
}
11441149
};
11451150
}

0 commit comments

Comments
 (0)