BrightSide Workbench Full Report + Source Code
CalendarUtil.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.html.calendar;
19 
20 import java.text.SimpleDateFormat;
21 import java.util.ArrayList;
22 import java.util.Calendar;
23 import java.util.List;
24 import java.util.Locale;
25 
30 public class CalendarUtil {
31 
33  private CalendarUtil() {
34  }
35 
36  public static void positionFirstCell(Calendar date) {
37  date.set(Calendar.DAY_OF_MONTH, 1);
38  int first = date.getFirstDayOfWeek();
39  while(date.get(Calendar.DAY_OF_WEEK) != first)
40  date.add(Calendar.DAY_OF_MONTH, -1);
41  }
42 
43  public static String[] getDayNames(Locale locale) {
44  return getDayNames(locale, false);
45  }
46 
47  public static String[] getDayNames(Locale locale, boolean small) {
48  Calendar date = Calendar.getInstance(locale);
49  SimpleDateFormat fmtDayWeek = new SimpleDateFormat((small ? "EE" : "EEEE"), locale);
50  int curr = date.getFirstDayOfWeek(), idx = 0;
51  date.set(Calendar.DAY_OF_WEEK, curr);
52  List dayNames = new ArrayList();
53  while(true) {
54  dayNames.add(fmtDayWeek.format(date.getTime()));
55  idx++; curr++;
56  if(idx >= 7) break;
57  if(curr > 7) curr = 1;
58  date.set(Calendar.DAY_OF_WEEK, curr);
59  }
60  return (String[])dayNames.toArray(new String[0]);
61  }
62 
63  public static String[] getMonthNames(Locale locale) {
64  return getMonthNames(locale, false);
65  }
66 
67  public static String[] getMonthNames(Locale locale, boolean small) {
68  Calendar date = Calendar.getInstance(locale);
69  SimpleDateFormat fmtMonth = new SimpleDateFormat((small ? "MMM" : "MMMM"), locale);
70  List monthNames = new ArrayList();
71  for(int m = 0; m < 12; m++) {
72  date.set(Calendar.MONTH, m);
73  monthNames.add(fmtMonth.format(date.getTime()));
74  }
75  return (String[])monthNames.toArray(new String[0]);
76  }
77 
78 }
static String[] getMonthNames(Locale locale, boolean small)
static String[] getDayNames(Locale locale, boolean small)
static String[] getDayNames(Locale locale)
static String[] getMonthNames(Locale locale)
static void positionFirstCell(Calendar date)