convert Formatter to kotlin
This commit is contained in:
parent
faaecbc229
commit
ab6fd0b9cc
|
@ -1,87 +0,0 @@
|
|||
package com.simplemobiletools.calendar;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.DateTimeZone;
|
||||
import org.joda.time.format.DateTimeFormat;
|
||||
import org.joda.time.format.DateTimeFormatter;
|
||||
|
||||
public class Formatter {
|
||||
public static final String DAYCODE_PATTERN = "YYYYMMdd";
|
||||
public static final String YEAR_PATTERN = "YYYY";
|
||||
private static final String DAY_PATTERN = "d";
|
||||
private static final String DAY_OF_WEEK_PATTERN = "EEE";
|
||||
private static final String EVENT_DATE_PATTERN = "d YYYY"; // MMMM doesn't give the proper month name in some languages
|
||||
private static final String EVENT_TIME_PATTERN = "HH:mm";
|
||||
|
||||
public static String getEventDate(Context context, String dayCode) {
|
||||
final DateTime dateTime = getDateTimeFromCode(dayCode);
|
||||
final String day = dateTime.toString(DAY_PATTERN);
|
||||
final String year = dateTime.toString(YEAR_PATTERN);
|
||||
final int monthIndex = Integer.valueOf(dayCode.substring(4, 6)) - 1;
|
||||
final String month = getMonthName(context, monthIndex);
|
||||
String date = month + " " + day;
|
||||
if (!year.equals(new DateTime().toString(YEAR_PATTERN)))
|
||||
date += " " + year;
|
||||
return date;
|
||||
}
|
||||
|
||||
public static String getEventDate(Context context, DateTime dateTime) {
|
||||
return getDayTitle(context, getDayCodeFromDateTime(dateTime));
|
||||
}
|
||||
|
||||
public static String getDayTitle(Context context, String dayCode) {
|
||||
String date = getEventDate(context, dayCode);
|
||||
final DateTime dateTime = getDateTimeFromCode(dayCode);
|
||||
final String day = dateTime.toString(DAY_OF_WEEK_PATTERN);
|
||||
return date + " (" + day + ")";
|
||||
}
|
||||
|
||||
public static String getEventTime(DateTime dateTime) {
|
||||
return dateTime.toString(EVENT_TIME_PATTERN);
|
||||
}
|
||||
|
||||
public static DateTime getDateTimeFromCode(String dayCode) {
|
||||
final DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(DAYCODE_PATTERN).withZone(DateTimeZone.UTC);
|
||||
return dateTimeFormatter.parseDateTime(dayCode);
|
||||
}
|
||||
|
||||
public static DateTime getLocalDateTimeFromCode(String dayCode) {
|
||||
final DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(DAYCODE_PATTERN).withZone(DateTimeZone.getDefault());
|
||||
return dateTimeFormatter.parseDateTime(dayCode);
|
||||
}
|
||||
|
||||
public static String getTime(int ts) {
|
||||
final DateTime dateTime = getDateTimeFromTS(ts);
|
||||
return getEventTime(dateTime);
|
||||
}
|
||||
|
||||
public static int getDayStartTS(String dayCode) {
|
||||
final DateTime dateTime = getLocalDateTimeFromCode(dayCode);
|
||||
return (int) (dateTime.getMillis() / 1000);
|
||||
}
|
||||
|
||||
public static int getDayEndTS(String dayCode) {
|
||||
final DateTime dateTime = getLocalDateTimeFromCode(dayCode);
|
||||
return (int) (dateTime.plusDays(1).minusMinutes(1).getMillis() / 1000);
|
||||
}
|
||||
|
||||
public static String getDayCodeFromTS(int ts) {
|
||||
final DateTime dateTime = getDateTimeFromTS(ts);
|
||||
return dateTime.toString(Formatter.DAYCODE_PATTERN);
|
||||
}
|
||||
|
||||
public static String getDayCodeFromDateTime(DateTime dateTime) {
|
||||
return dateTime.toString(Formatter.DAYCODE_PATTERN);
|
||||
}
|
||||
|
||||
public static DateTime getDateTimeFromTS(int ts) {
|
||||
return new DateTime(ts * 1000L, DateTimeZone.getDefault());
|
||||
}
|
||||
|
||||
// use manually translated month names, as DateFormat and Joda have issues with a lot of languages
|
||||
public static String getMonthName(Context context, int id) {
|
||||
return context.getResources().getStringArray(R.array.months)[id];
|
||||
}
|
||||
}
|
|
@ -46,7 +46,7 @@ public class Utils {
|
|||
}
|
||||
|
||||
private static int getNewTS(int ts, boolean isMonthly) {
|
||||
DateTime dateTime = Formatter.getDateTimeFromTS(ts);
|
||||
DateTime dateTime = Formatter.INSTANCE.getDateTimeFromTS(ts);
|
||||
while (dateTime.isBeforeNow()) {
|
||||
dateTime = isMonthly ? dateTime.plusMonths(1) : dateTime.plusYears(1);
|
||||
}
|
||||
|
|
|
@ -31,8 +31,8 @@ public class NotificationReceiver extends BroadcastReceiver {
|
|||
return;
|
||||
|
||||
final PendingIntent pendingIntent = getPendingIntent(context, event);
|
||||
final String startTime = Formatter.getTime(event.getStartTS());
|
||||
final String endTime = Formatter.getTime(event.getEndTS());
|
||||
final String startTime = Formatter.INSTANCE.getTime(event.getStartTS());
|
||||
final String endTime = Formatter.INSTANCE.getTime(event.getEndTS());
|
||||
final String title = event.getTitle();
|
||||
final Notification notification = getNotification(context, pendingIntent, getEventTime(startTime, endTime) + " " + title);
|
||||
notificationManager.notify(id, notification);
|
||||
|
|
|
@ -0,0 +1,56 @@
|
|||
package com.simplemobiletools.calendar
|
||||
|
||||
import android.content.Context
|
||||
import org.joda.time.DateTime
|
||||
import org.joda.time.DateTimeZone
|
||||
import org.joda.time.format.DateTimeFormat
|
||||
|
||||
object Formatter {
|
||||
val DAYCODE_PATTERN = "YYYYMMdd"
|
||||
val YEAR_PATTERN = "YYYY"
|
||||
private val DAY_PATTERN = "d"
|
||||
private val DAY_OF_WEEK_PATTERN = "EEE"
|
||||
private val EVENT_TIME_PATTERN = "HH:mm"
|
||||
|
||||
fun getEventDate(context: Context, dayCode: String): String {
|
||||
val dateTime = getDateTimeFromCode(dayCode)
|
||||
val day = dateTime.toString(DAY_PATTERN)
|
||||
val year = dateTime.toString(YEAR_PATTERN)
|
||||
val monthIndex = Integer.valueOf(dayCode.substring(4, 6))!! - 1
|
||||
val month = getMonthName(context, monthIndex)
|
||||
var date = month + " " + day
|
||||
if (year != DateTime().toString(YEAR_PATTERN))
|
||||
date += " " + year
|
||||
return date
|
||||
}
|
||||
|
||||
fun getDayTitle(context: Context, dayCode: String): String {
|
||||
val date = getEventDate(context, dayCode)
|
||||
val dateTime = getDateTimeFromCode(dayCode)
|
||||
val day = dateTime.toString(DAY_OF_WEEK_PATTERN)
|
||||
return "$date ($day)"
|
||||
}
|
||||
|
||||
fun getEventDate(context: Context, dateTime: DateTime) = getDayTitle(context, getDayCodeFromDateTime(dateTime))
|
||||
|
||||
fun getEventTime(dateTime: DateTime) = dateTime.toString(EVENT_TIME_PATTERN)
|
||||
|
||||
fun getDateTimeFromCode(dayCode: String) = DateTimeFormat.forPattern(DAYCODE_PATTERN).withZone(DateTimeZone.UTC).parseDateTime(dayCode)
|
||||
|
||||
fun getLocalDateTimeFromCode(dayCode: String) = DateTimeFormat.forPattern(DAYCODE_PATTERN).withZone(DateTimeZone.getDefault()).parseDateTime(dayCode)
|
||||
|
||||
fun getTime(ts: Int) = getEventTime(getDateTimeFromTS(ts))
|
||||
|
||||
fun getDayStartTS(dayCode: String) = (getLocalDateTimeFromCode(dayCode).millis / 1000).toInt()
|
||||
|
||||
fun getDayEndTS(dayCode: String) = (getLocalDateTimeFromCode(dayCode).plusDays(1).minusMinutes(1).millis / 1000).toInt()
|
||||
|
||||
fun getDayCodeFromTS(ts: Int) = getDateTimeFromTS(ts).toString(Formatter.DAYCODE_PATTERN)
|
||||
|
||||
fun getDayCodeFromDateTime(dateTime: DateTime) = dateTime.toString(Formatter.DAYCODE_PATTERN)
|
||||
|
||||
fun getDateTimeFromTS(ts: Int) = DateTime(ts * 1000L, DateTimeZone.getDefault())
|
||||
|
||||
// use manually translated month names, as DateFormat and Joda have issues with a lot of languages
|
||||
fun getMonthName(context: Context, id: Int) = context.resources.getStringArray(R.array.months)[id]
|
||||
}
|
Loading…
Reference in New Issue