Kotlin to Java Conversion: WeekViewUtil Class
import android.content.Context; import android.os.Build; import android.text.format.DateFormat; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Locale;
public class WeekViewUtil { ///////////////////////////////////////////////////////////////// // // Helper methods. // /////////////////////////////////////////////////////////////////
/**
* Checks if two dates are on the same day.
*
* @param dateOne The first date.
* @param dateTwo The second date. *
* @return Whether the dates are on the same day.
*/
public static boolean isSameDay(Calendar dateOne, Calendar dateTwo) {
if (dateOne == dateTwo)
return true;
return dateOne.get(Calendar.YEAR) == dateTwo.get(Calendar.YEAR) && dateOne.get(Calendar.DAY_OF_YEAR) == dateTwo.get(Calendar.DAY_OF_YEAR);
}
/**
* Returns a calendar instance at the start of today
*
* @return the calendar instance
*/
public static Calendar today() {
Calendar today = Calendar.getInstance();
today.set(Calendar.HOUR_OF_DAY, 0);
today.set(Calendar.MINUTE, 0);
today.set(Calendar.SECOND, 0);
today.set(Calendar.MILLISECOND, 0);
return today;
}
public static boolean isSameDayAndHourAndMinute(Calendar dateOne, Calendar dateTwo) {
return isSameDay(dateOne, dateTwo) && dateOne.get(Calendar.HOUR_OF_DAY) == dateTwo.get(Calendar.HOUR_OF_DAY)
&& dateOne.get(Calendar.MINUTE) == dateTwo.get(Calendar.MINUTE);
}
/**
* Returns the amount of days between the second date and the first date
*
* @param dateOne the first date
* @param dateTwo the second date
* @return the amount of days between dateTwo and dateOne
*/
public static int daysBetween(Calendar dateOne, Calendar dateTwo) {
return (int) (((dateTwo.getTimeInMillis() + dateTwo.getTimeZone().getOffset(dateTwo.getTimeInMillis())) / (1000 * 60 * 60 * 24))
- ((dateOne.getTimeInMillis() + dateOne.getTimeZone().getOffset(dateOne.getTimeInMillis())) / (1000 * 60 * 60 * 24)));
}
/*
* Returns the amount of minutes passed in the day before the time in the given date
* @param date
* @return amount of minutes in day before time
*/
public static int getPassedMinutesInDay(Calendar date) {
return getPassedMinutesInDay(date.get(Calendar.HOUR_OF_DAY), date.get(Calendar.MINUTE));
}
/**
* Returns the amount of minutes in the given hours and minutes
*
* @param hour
* @param minute
* @return amount of minutes in the given hours and minutes
*/
public static int getPassedMinutesInDay(int hour, int minute) {
return hour * 60 + minute;
}
/**
* returns a date format of dayOfWeek+day&month, based on the current locale.
* This is important, as the format is different in many countries. Especially the numeric part that can be different : 'd/M', 'M/d', 'd-M', 'M-d' ,...
*/
public static java.text.DateFormat getWeekdayWithNumericDayAndMonthFormat(Context context, boolean shortDate) {
String weekDayFormat = shortDate ? 'EEEEE' : 'EEE';
String defaultDateFormatPattern = weekDayFormat + ' d/M';
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
Locale locale = Locale.getDefault();
String bestDateTimePattern = DateFormat.getBestDateTimePattern(locale, defaultDateFormatPattern);
// workaround fix for this issue: https://issuetracker.google.com/issues/79311044
// TODO if there is a better API that doesn't require this workaround, use it. Be sure to check vs all locales, as done here: https://issuetracker.google.com/issues/37044127
bestDateTimePattern = bestDateTimePattern.replace('d+'.toRegex(), 'd').replace('M+'.toRegex(), 'M');
bestDateTimePattern = bestDateTimePattern.replace('E+'.toRegex(), weekDayFormat);
return new SimpleDateFormat(bestDateTimePattern, locale);
}
try {
char[] dateFormatOrder = DateFormat.getDateFormatOrder(context);
if (dateFormatOrder.length == 0)
return new SimpleDateFormat(defaultDateFormatPattern, Locale.getDefault());
StringBuilder sb = new StringBuilder();
for (int i = 0; i < dateFormatOrder.length; i++) {
char c = dateFormatOrder[i];
if (Character.toLowerCase(c) == 'y')
continue;
if (sb.length() > 0)
sb.append('/');
switch (Character.toLowerCase(c)) {
case 'm':
sb.append('M');
break;
case 'd':
sb.append('d');
break;
}
}
String dateFormatString = sb.toString();
return new SimpleDateFormat(weekDayFormat + ' ' + dateFormatString, Locale.getDefault());
} catch (Exception e) {
return new SimpleDateFormat(defaultDateFormatPattern, Locale.getDefault());
}
}
public static String calendarToString(Calendar cal, boolean includeTime) {
if (cal == null)
return '';
StringBuilder sb = new StringBuilder();
sb.append(cal.get(Calendar.YEAR)).append('-').append((cal.get(Calendar.MONTH) + 1))
.append('-').append(cal.get(Calendar.DAY_OF_MONTH));
if (includeTime)
sb.append(' ').append(cal.get(Calendar.HOUR_OF_DAY)).append(':').append(cal.get(Calendar.MINUTE)).append(':')
.append(cal.get(Calendar.SECOND)).append('.').append(cal.get(Calendar.MILLISECOND));
return sb.toString();
}
public static void resetTime(Calendar cal) {
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.MILLISECOND, 0);
}
}
原文地址: https://www.cveoy.top/t/topic/fxEj 著作权归作者所有。请勿转载和采集!