84
84
import com .google .android .material .snackbar .Snackbar ;
85
85
import com .google .android .material .tabs .TabLayout ;
86
86
87
+ import java .text .DateFormat ;
87
88
import java .text .ParseException ;
88
89
import java .text .SimpleDateFormat ;
89
90
import java .util .Calendar ;
90
91
import java .util .Date ;
92
+ import java .util .Locale ;
91
93
import java .util .Objects ;
94
+ import java .util .TimeZone ;
92
95
93
96
public class DataPlanFragment extends Fragment {
94
97
public static final String TAG = DataPlanFragment .class .getSimpleName ();
@@ -101,6 +104,7 @@ public class DataPlanFragment extends Fragment {
101
104
private Long planStartDateMillis , planEndDateMillis ;
102
105
private int startHour , startMinute , endHour , endMinute ;
103
106
private long startMillis , endMillis ; // Absolute start and end time in millis
107
+ private boolean is12HourView ;
104
108
105
109
@ Override
106
110
public void onCreate (@ Nullable Bundle savedInstanceState ) {
@@ -131,11 +135,16 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
131
135
Calendar calendar = Calendar .getInstance ();
132
136
int daysInMonth = calendar .getActualMaximum (Calendar .DAY_OF_MONTH );
133
137
138
+ Date date = new Date ();
139
+ String time = DateFormat .getTimeInstance (DateFormat .SHORT ).format (date .getTime ()).toLowerCase (Locale .ROOT );
140
+ is12HourView = time .contains ("am" ) || time .contains ("pm" ) ||
141
+ time .contains ("a.m" ) || time .contains ("p.m" );
142
+
134
143
try {
135
144
planStartDateMillis = PreferenceManager .getDefaultSharedPreferences (requireContext ())
136
- .getLong (DATA_RESET_CUSTOM_DATE_START , MaterialDatePicker .todayInUtcMilliseconds ());
145
+ .getLong (DATA_RESET_CUSTOM_DATE_START , UTCToLocal ( MaterialDatePicker .todayInUtcMilliseconds () ));
137
146
planEndDateMillis = PreferenceManager .getDefaultSharedPreferences (requireContext ())
138
- .getLong (DATA_RESET_CUSTOM_DATE_END , MaterialDatePicker .todayInUtcMilliseconds ());
147
+ .getLong (DATA_RESET_CUSTOM_DATE_END , UTCToLocal ( MaterialDatePicker .todayInUtcMilliseconds () ));
139
148
}
140
149
catch (ClassCastException e ) {
141
150
int planStartIntValue = PreferenceManager .getDefaultSharedPreferences (requireContext ())
@@ -165,16 +174,16 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
165
174
String planStart = new SimpleDateFormat ("dd/MM/yyyy" ).format (planStartDateMillis );
166
175
String planEnd = new SimpleDateFormat ("dd/MM/yyyy" ).format (planEndDateMillis );
167
176
String startTime , endTime ;
168
- startTime = getContext ().getString (R .string .label_custom_start_time , getTime (startHour , startMinute ));
169
- endTime = getContext ().getString (R .string .label_custom_end_time , getTime (endHour , endMinute ));
177
+ startTime = getContext ().getString (R .string .label_custom_start_time , getTime (startHour , startMinute , is12HourView ));
178
+ endTime = getContext ().getString (R .string .label_custom_end_time , getTime (endHour , endMinute , is12HourView ));
170
179
String startDateToday = getContext ().getString (R .string .label_custom_start_date , planStart );
171
180
String endDateToday = getContext ().getString (R .string .label_custom_end_date , planEnd );
172
181
173
182
174
183
binding .customStartDate .setText (setBoldSpan (startDateToday , planStart ));
175
184
binding .customEndDate .setText (setBoldSpan (endDateToday , planEnd ));
176
- binding .customStartTime .setText (setBoldSpan (startTime , getTime (startHour , startMinute )));
177
- binding .customEndTime .setText (setBoldSpan (endTime , getTime (endHour , endMinute )));
185
+ binding .customStartTime .setText (setBoldSpan (startTime , getTime (startHour , startMinute , is12HourView )));
186
+ binding .customEndTime .setText (setBoldSpan (endTime , getTime (endHour , endMinute , is12HourView )));
178
187
179
188
binding .customStartDate .setOnClickListener (new View .OnClickListener () {
180
189
@ Override
@@ -202,10 +211,14 @@ public void onClick(View v) {
202
211
@ Override
203
212
public void onPositiveButtonClick (Object selection ) {
204
213
planStartDateMillis = Long .parseLong (selection .toString ());
205
- Log .d (TAG , "onPositiveButtonClick: " + planStartDateMillis );
206
- Log .d (TAG , "onPositiveButtonClick: " + UTCToLocal (planStartDateMillis ));
214
+ Log .d (TAG , "onPositiveButtonClick: UTC: " + planStartDateMillis );
215
+ Log .d (TAG , "onPositiveButtonClick: Local: " + UTCToLocal (planStartDateMillis ));
207
216
planStartDateMillis = UTCToLocal (planStartDateMillis );
208
- String date = new SimpleDateFormat ("dd/MM/yyyy" ).format (planStartDateMillis );
217
+ Log .d (TAG , "onPositiveButtonClick: UTC: " + localToUTC (planStartDateMillis ));
218
+ SimpleDateFormat dateFormat = new SimpleDateFormat ("dd/MM/yyyy" );
219
+ dateFormat .setTimeZone (TimeZone .getDefault ());
220
+ String date = dateFormat .format (new Date (planStartDateMillis ));
221
+
209
222
String startDateString = getContext ().getString (R .string .label_custom_start_date , date );
210
223
binding .customStartDate .setText (setBoldSpan (startDateString , date ));
211
224
}
@@ -240,7 +253,7 @@ public void onClick(View v) {
240
253
@ Override
241
254
public void onPositiveButtonClick (Object selection ) {
242
255
planEndDateMillis = Long .parseLong (selection .toString ());
243
- planEndDateMillis = UTCToLocal (planEndDateMillis ) + 86399999 ; // 86,399,999 is added to change the time to 23:59:59
256
+ planEndDateMillis = UTCToLocal (planEndDateMillis );
244
257
String date = new SimpleDateFormat ("dd/MM/yyyy" ).format (planEndDateMillis );
245
258
String endDateString = getContext ().getString (R .string .label_custom_end_date , date );
246
259
binding .customEndDate .setText (setBoldSpan (endDateString , date ));
@@ -371,8 +384,10 @@ public void onClick(View v) {
371
384
e .printStackTrace ();
372
385
}
373
386
374
- if (binding .dataReset .getCheckedRadioButtonId () == R .id .custom_reset &&
375
- startMillis > System .currentTimeMillis () || endMillis < System .currentTimeMillis ()) {
387
+ if ((binding .dataReset .getCheckedRadioButtonId () == R .id .custom_reset &&
388
+ startMillis > System .currentTimeMillis ()) ||
389
+ (binding .dataReset .getCheckedRadioButtonId () == R .id .custom_reset &&
390
+ endMillis < System .currentTimeMillis ())) {
376
391
Snackbar snackbar = Snackbar .make (binding .getRoot (),
377
392
requireContext ().getString (R .string .error_invalid_plan_duration ),
378
393
Snackbar .LENGTH_SHORT );
@@ -483,6 +498,8 @@ private void showTimePicker(int type) {
483
498
(((LinearLayout ) ((LinearLayout ) timePicker .getChildAt (0 )).getChildAt (0 )).getChildAt (0 )).setVerticalScrollBarEnabled (false );
484
499
(((LinearLayout ) ((LinearLayout ) timePicker .getChildAt (0 )).getChildAt (0 )).getChildAt (2 )).setVerticalScrollBarEnabled (false );
485
500
501
+ timePicker .setIs24HourView (!is12HourView );
502
+
486
503
if (type == TYPE_PLAN_START ) {
487
504
timePicker .setHour (startHour );
488
505
timePicker .setMinute (startMinute );
@@ -523,7 +540,7 @@ public void onClick(View view) {
523
540
524
541
String time , timeText ;
525
542
526
- time = getTime (timePicker .getHour (), timePicker .getMinute ());
543
+ time = getTime (timePicker .getHour (), timePicker .getMinute (), is12HourView );
527
544
528
545
if (type == TYPE_PLAN_START ) {
529
546
timeText = getContext ().getString (R .string .label_custom_start_time , time );
@@ -556,52 +573,68 @@ public void onShow(DialogInterface dialogInterface) {
556
573
dialog .show ();
557
574
}
558
575
559
- private String getTime (int hour , int minute ) {
576
+ private String getTime (int hour , int minute , boolean is12HourView ) {
560
577
String time ;
561
-
562
578
int hourOfDay ;
563
-
564
- if ( hour >= 12 ) {
565
- if (hour == 12 ) {
566
- hourOfDay = 12 ;
579
+ if (! is12HourView ) {
580
+ String formattedHour , formattedMinute ;
581
+ if (hour < 10 ) {
582
+ formattedHour = "0" + hour ;
567
583
}
568
584
else {
569
- hourOfDay = ( hour - 12 ) ;
585
+ formattedHour = "" + hour ;
570
586
}
571
587
if (minute < 10 ) {
572
- time = hourOfDay + ": 0" + minute + " pm" ;
588
+ formattedMinute = " 0" + minute ;
573
589
}
574
590
else {
575
- time = hourOfDay + ": " + minute + " pm" ;
591
+ formattedMinute = " " + minute ;
576
592
}
593
+ time = formattedHour + ":" + formattedMinute ;
577
594
}
578
595
else {
579
- if (hour == 0 ) {
580
- hourOfDay = 12 ;
581
- }
582
- else if (hour < 10 ) {
583
- hourOfDay = hour ;
596
+ if (hour >= 12 ) {
597
+ if (hour == 12 ) {
598
+ hourOfDay = 12 ;
599
+ }
600
+ else {
601
+ hourOfDay = (hour - 12 );
602
+ }
584
603
if (minute < 10 ) {
585
- time = "0" + hourOfDay + ":0" + minute + " pm" ;
604
+ time = hourOfDay + ":0" + minute + " pm" ;
586
605
}
587
606
else {
588
- time = "0" + hourOfDay + ":" + minute + " pm" ;
607
+ time = hourOfDay + ":" + minute + " pm" ;
589
608
}
590
609
}
591
610
else {
592
- hourOfDay = hour ;
593
- }
594
- if (hourOfDay < 10 ) {
595
- time = "0" + hourOfDay + ":" + minute + " am" ;
596
- }
597
- else {
598
- time = hourOfDay + ":" + minute + " am" ;
599
- }
600
- if (minute < 10 ) {
601
- time = hourOfDay + ":0" + minute + " am" ;
602
- }
603
- else {
604
- time = hourOfDay + ":" + minute + " am" ;
611
+ if (hour == 0 ) {
612
+ hourOfDay = 12 ;
613
+ }
614
+ else if (hour < 10 ) {
615
+ hourOfDay = hour ;
616
+ if (minute < 10 ) {
617
+ time = "0" + hourOfDay + ":0" + minute + " pm" ;
618
+ }
619
+ else {
620
+ time = "0" + hourOfDay + ":" + minute + " pm" ;
621
+ }
622
+ }
623
+ else {
624
+ hourOfDay = hour ;
625
+ }
626
+ if (hourOfDay < 10 ) {
627
+ time = "0" + hourOfDay + ":" + minute + " am" ;
628
+ }
629
+ else {
630
+ time = hourOfDay + ":" + minute + " am" ;
631
+ }
632
+ if (minute < 10 ) {
633
+ time = hourOfDay + ":0" + minute + " am" ;
634
+ }
635
+ else {
636
+ time = hourOfDay + ":" + minute + " am" ;
637
+ }
605
638
}
606
639
}
607
640
return time ;
0 commit comments