From ff5a3511d5e683fa015fdb8d1e91b17b9f91bd53 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Sun, 19 Apr 2020 05:09:03 +0200 Subject: [PATCH] Continued: - moved from jcontacts-business-core as this is very generic MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../mxchange/jcoreee/dates/DayOfTheWeek.java | 140 ++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 src/org/mxchange/jcoreee/dates/DayOfTheWeek.java diff --git a/src/org/mxchange/jcoreee/dates/DayOfTheWeek.java b/src/org/mxchange/jcoreee/dates/DayOfTheWeek.java new file mode 100644 index 0000000..7106028 --- /dev/null +++ b/src/org/mxchange/jcoreee/dates/DayOfTheWeek.java @@ -0,0 +1,140 @@ +/* + * Copyright (C) 2016 - 2020 Free Software Foundation + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.jcoreee.dates; + +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; +import java.util.GregorianCalendar; + +/** + * An enumeration suitable for persisting + *

+ * @author Roland Häder + */ +public enum DayOfTheWeek { + SUNDAY { + @Override + public int toCalendar () { + return Calendar.SUNDAY; + } + + }, + MONDAY { + @Override + public int toCalendar () { + return Calendar.MONDAY; + } + }, + TUESDAY { + @Override + public int toCalendar () { + return Calendar.TUESDAY; + } + }, + WEDNESDAY { + @Override + public int toCalendar () { + return Calendar.WEDNESDAY; + } + }, + THURSDAY { + @Override + public int toCalendar () { + return Calendar.THURSDAY; + } + }, + FRIDAY { + @Override + public int toCalendar () { + return Calendar.FRIDAY; + } + }, + SATURDAY { + @Override + public int toCalendar () { + return Calendar.SATURDAY; + } + }; + + /** + * Simple date format instance + */ + private static final SimpleDateFormat SIMPLE_DATE_FORMAT; + + static { + // Init SDF once + SIMPLE_DATE_FORMAT = (SimpleDateFormat) SimpleDateFormat.getInstance(); + + // Set pattern + SIMPLE_DATE_FORMAT.applyPattern("EEEEEEEEEE"); //NOI18N + } + + public abstract int toCalendar (); + + /** + * Returns the DateOfTheWeek instance by given day number (0-6) + *

+ * @param day Day number (0-6) + *

+ * @return DateOfTheWeek instance + */ + public static DayOfTheWeek fromCalendarDay (final int day) { + // Walk through all values + for (final DayOfTheWeek dayOfWeek : DayOfTheWeek.values()) { + if (dayOfWeek.toCalendar() == day) { + return dayOfWeek; + } + } + + return null; // Consider throwing IllegalArgumentException + } + + /** + * Returns the DateOfTheWeek instance by given Date instance + *

+ * @param date Date instance + *

+ * @return DateOfTheWeek instance + */ + public static DayOfTheWeek getByDate (final Date date) { + // Init calendar + final Calendar calendar = GregorianCalendar.getInstance(); + + // Set time + calendar.setTime(date); + + // Return DayOfTheWeek + return fromCalendarDay(calendar.get(Calendar.DAY_OF_WEEK)); + } + + /* + * Should return the localized day of the week + */ + @Override + public String toString () { + // Init calendar + final Calendar calendar = new GregorianCalendar(); + + // Set DAY_OF_WEEK + calendar.set(Calendar.DAY_OF_WEEK, this.toCalendar()); + + // Format string + return SIMPLE_DATE_FORMAT.format(calendar.getTime()); + } + +} -- 2.39.5