]> git.mxchange.org Git - jcontacts-business-core.git/blob - src/org/mxchange/jcontactsbusiness/model/opening_time/dayofweek/DayOfTheWeek.java
Updated jar(s)
[jcontacts-business-core.git] / src / org / mxchange / jcontactsbusiness / model / opening_time / dayofweek / DayOfTheWeek.java
1 /*
2  * Copyright (C) 2016 - 2018 Free Software Foundation
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.jcontactsbusiness.model.opening_time.dayofweek;
18
19 import java.text.SimpleDateFormat;
20 import java.util.Calendar;
21 import java.util.Date;
22 import java.util.GregorianCalendar;
23
24 /**
25  * An enumeration suitable for persisting
26  * <p>
27  * @author Roland Häder<roland@mxchange.org>
28  */
29 public enum DayOfTheWeek {
30         SUNDAY {
31                 @Override
32                 public int toCalendar () {
33                         return Calendar.SUNDAY;
34                 }
35
36         },
37         MONDAY {
38                 @Override
39                 public int toCalendar () {
40                         return Calendar.MONDAY;
41                 }
42         },
43         TUESDAY {
44                 @Override
45                 public int toCalendar () {
46                         return Calendar.TUESDAY;
47                 }
48         },
49         WEDNESDAY {
50                 @Override
51                 public int toCalendar () {
52                         return Calendar.WEDNESDAY;
53                 }
54         },
55         THURSDAY {
56                 @Override
57                 public int toCalendar () {
58                         return Calendar.THURSDAY;
59                 }
60         },
61         FRIDAY {
62                 @Override
63                 public int toCalendar () {
64                         return Calendar.FRIDAY;
65                 }
66         },
67         SATURDAY {
68                 @Override
69                 public int toCalendar () {
70                         return Calendar.SATURDAY;
71                 }
72         };
73
74         public abstract int toCalendar ();
75
76         public static DayOfTheWeek fromCalendarDay (final int day) {
77
78                 for (DayOfTheWeek dayOfWeek : DayOfTheWeek.values()) {
79                         if (dayOfWeek.toCalendar() == day) {
80                                 return dayOfWeek;
81                         }
82                 }
83
84                 return null; // Consider throwing IllegalArgumentException
85         }
86
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));
91         }
92
93         /*
94          * Should return the localized day of the week
95          */
96         @Override
97         public String toString () {
98                 Calendar c = new GregorianCalendar();
99                 c.set(Calendar.DAY_OF_WEEK, this.toCalendar());
100                 SimpleDateFormat sdf = (SimpleDateFormat) SimpleDateFormat
101                                                  .getInstance();
102                 sdf.applyPattern("EEEEEEEEEE"); //NOI18N
103
104                 return sdf.format(c.getTime());
105         }
106
107 }