2 * Copyright (C) 2016 - 2018 Free Software Foundation
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package org.mxchange.jcontactsbusiness.model.opening_time.dayofweek;
19 import java.text.SimpleDateFormat;
20 import java.util.Calendar;
21 import java.util.Date;
22 import java.util.GregorianCalendar;
25 * An enumeration suitable for persisting
27 * @author Roland Häder<roland@mxchange.org>
29 public enum DayOfTheWeek {
32 public int toCalendar () {
33 return Calendar.SUNDAY;
39 public int toCalendar () {
40 return Calendar.MONDAY;
45 public int toCalendar () {
46 return Calendar.TUESDAY;
51 public int toCalendar () {
52 return Calendar.WEDNESDAY;
57 public int toCalendar () {
58 return Calendar.THURSDAY;
63 public int toCalendar () {
64 return Calendar.FRIDAY;
69 public int toCalendar () {
70 return Calendar.SATURDAY;
74 public abstract int toCalendar ();
76 public static DayOfTheWeek fromCalendarDay (final int day) {
78 for (DayOfTheWeek dayOfWeek : DayOfTheWeek.values()) {
79 if (dayOfWeek.toCalendar() == day) {
84 return null; // Consider throwing IllegalArgumentException
87 public static DayOfTheWeek getByDate (final Date date) {
88 Calendar calendar = GregorianCalendar.getInstance();
89 calendar.setTime(date);
90 return fromCalendarDay(calendar.get(Calendar.DAY_OF_WEEK));
94 * Should return the localized day of the week
97 public String toString () {
98 Calendar c = new GregorianCalendar();
99 c.set(Calendar.DAY_OF_WEEK, this.toCalendar());
100 SimpleDateFormat sdf = (SimpleDateFormat) SimpleDateFormat
102 sdf.applyPattern("EEEEEEEEEE"); //NOI18N
104 return sdf.format(c.getTime());