BrightSide Workbench Full Report + Source Code
DateFormats.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.elephant.util;
19 
20 import java.text.DateFormat;
21 import java.text.DateFormatSymbols;
22 import java.text.SimpleDateFormat;
23 import java.util.Date;
24 import org.amic.util.date.CheckDate;
25 import org.turro.elephant.context.Application;
26 
31 public class DateFormats {
32 
33  public static final String format(Date d, boolean dateOnly) {
34  if(d == null) return null;
35  if(dateOnly) {
36  return org.amic.util.date.DateFormats.format(d,
37  DateFormat.SHORT, Application.getUsedLocale());
38  } else {
39  return org.amic.util.date.DateFormats.format(d,
40  DateFormat.SHORT, DateFormat.SHORT, Application.getUsedLocale());
41  }
42  }
43 
44  public static String formatNull(Date d, boolean dateOnly) {
45  if(d == null) {
46  return "";
47  }
48  return DateFormats.format(d, dateOnly);
49  }
50 
51  public static String getDefaultFormat() {
52  final DateFormat df = DateFormat.getDateInstance(
53  DateFormat.SHORT, Application.getUsedLocale());
54  if (df instanceof SimpleDateFormat) {
55  final String fmt = ((SimpleDateFormat)df).toPattern();
56  if (fmt != null && !"M/d/yy h:mm a".equals(fmt))
57  return fmt; //note: JVM use "M/d/yy h:mm a" if not found!
58  }
59  return "yyyy/MM/dd";
60  }
61 
62  public static String getDefaultDateTimeFormat() {
63  final DateFormat df = DateFormat.getDateTimeInstance(
64  DateFormat.SHORT, DateFormat.SHORT, Application.getUsedLocale());
65  if (df instanceof SimpleDateFormat) {
66  final String fmt = ((SimpleDateFormat)df).toPattern();
67  if (fmt != null && !"M/d/yy h:mm a".equals(fmt))
68  return fmt; //note: JVM use "M/d/yy h:mm a" if not found!
69  }
70  return "yyyy/MM/dd hh:mm a";
71  }
72 
73  public static String monthName(Date date) {
74  DateFormatSymbols dfs = DateFormatSymbols.getInstance(Application.getUsedLocale());
75  return dfs.getMonths()[new CheckDate(date).getMonth() - 1];
76  }
77 
78  public static String formatTime(Date d) {
79  if(d == null) {
80  return "";
81  }
82  final DateFormat df = DateFormat.getTimeInstance(
83  DateFormat.SHORT, Application.getUsedLocale());
84  return df.format(d);
85  }
86 
87 }
static String monthName(Date date)
static final String format(Date d, boolean dateOnly)
static String formatNull(Date d, boolean dateOnly)
static String formatTime(Date d)