I'm writing an app for android. On the start of it I create a Calendar, filling such containers as YEAR, MONTH, DAY_OF_MONTH, ERA and time zone as GMT.
I get something like this
java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=false,lenient=true,zone=java.util.SimpleTimeZone[id=GMT,offset=0,dstSavings=3600000,useDaylight=false,startYear=0,startMode=0,startMonth=0,startDay=0,startDayOfWeek=0,startTime=0,startTimeMode=0,endMode=0,endMonth=0,endDay=0,endDayOfWeek=0,endTime=0,endTimeMode=0],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2021,MONTH=3,WEEK_OF_YEAR=?,WEEK_OF_MONTH=?,DAY_OF_MONTH=14,DAY_OF_YEAR=?,DAY_OF_WEEK=?,DAY_OF_WEEK_IN_MONTH=?,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=0,SECOND=0,MILLISECOND=?,ZONE_OFFSET=?,DST_OFFSET=?]
Later on I need to get a string like "14-04-2021", so I create a Date variable using getTimeInMillies for the Calendar before. All the fields of it become filled with zeros.
After parsing a JSON, I need to create a map with Calendar keys. It looks like this.
String key = keys.next();
Date d = dateFormat.parse(key);
String rate = String.valueOf(rates.getJsonObject(key).get(get_curr_inf(get_curr_to_r())));
Double rate_ = Double.parseDouble(rate);
Calendar cal = new GregorianCalendar();
cal.setTime(d);
Calendar tmp = new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
tmp.setTimeZone(TimeZone.getTimeZone("GMT"));
tmp.set(Calendar.ERA, 1);
long f = tmp.getTimeInMillis();
data.put(tmp, rate_);
I do it in such a strange way as it needs to be exactly similar to the previous Calendar, cause i parse map using it.
Is there any way to make it look better?
Never use
Calendar. That terrible class was supplanted years ago by the modern java.time classes.For a date-only value without time-of-day and without time zone, use
LocalDate.The java.time classes are built into Android 26 and later. The latest Android tooling brings much of that functionality to earlier Android by way of API desugaring.