| 1 |
/* |
| 2 |
* Copyright 2004-2007 The Portal Application Laboratory Team. |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, |
| 13 |
* either express or implied. See the License for the specific language |
| 14 |
* governing permissions and limitations under the License. |
| 15 |
*/ |
| 16 |
package jp.sf.pal.common.faces.util; |
| 17 |
|
| 18 |
import java.text.DateFormat; |
| 19 |
import java.util.Date; |
| 20 |
import java.util.Locale; |
| 21 |
|
| 22 |
import javax.faces.component.UIViewRoot; |
| 23 |
import javax.faces.context.FacesContext; |
| 24 |
|
| 25 |
public class DateFormatUtil { |
| 26 |
public static String getLongFormattedDateTime(Date d) { |
| 27 |
return getFormattedDateTime(d, DateFormat.LONG, DateFormat.LONG); |
| 28 |
} |
| 29 |
|
| 30 |
public static String getMediumFormattedDateTime(Date d) { |
| 31 |
return getFormattedDateTime(d, DateFormat.MEDIUM, DateFormat.MEDIUM); |
| 32 |
} |
| 33 |
|
| 34 |
public static String getShortFormattedDateTime(Date d) { |
| 35 |
return getFormattedDateTime(d, DateFormat.SHORT, DateFormat.SHORT); |
| 36 |
} |
| 37 |
|
| 38 |
public static String getFormattedDateTime(Date d, int dateStyle, |
| 39 |
int timeStyle) |
| 40 |
|
| 41 |
{ |
| 42 |
UIViewRoot viewRoot = FacesContext.getCurrentInstance().getViewRoot(); |
| 43 |
Locale locale = null; |
| 44 |
if (viewRoot != null) { |
| 45 |
locale = viewRoot.getLocale(); |
| 46 |
} else { |
| 47 |
locale = Locale.ENGLISH; |
| 48 |
} |
| 49 |
|
| 50 |
DateFormat df = DateFormat.getDateTimeInstance(dateStyle, timeStyle, |
| 51 |
locale); |
| 52 |
return df.format(d); |
| 53 |
} |
| 54 |
|
| 55 |
public static String getLongFormattedDate(Date d) { |
| 56 |
return getFormattedDate(d, DateFormat.LONG); |
| 57 |
} |
| 58 |
|
| 59 |
public static String getMediumFormattedDate(Date d) { |
| 60 |
return getFormattedDate(d, DateFormat.MEDIUM); |
| 61 |
} |
| 62 |
|
| 63 |
public static String getShortFormattedDate(Date d) { |
| 64 |
return getFormattedDate(d, DateFormat.SHORT); |
| 65 |
} |
| 66 |
|
| 67 |
public static String getFormattedDate(Date d, int dateStyle) { |
| 68 |
UIViewRoot viewRoot = FacesContext.getCurrentInstance().getViewRoot(); |
| 69 |
Locale locale = null; |
| 70 |
if (viewRoot != null) { |
| 71 |
locale = viewRoot.getLocale(); |
| 72 |
} else { |
| 73 |
locale = Locale.ENGLISH; |
| 74 |
} |
| 75 |
|
| 76 |
DateFormat df = DateFormat.getDateInstance(dateStyle, locale); |
| 77 |
return df.format(d); |
| 78 |
} |
| 79 |
|
| 80 |
public static String getLongFormattedTime(Date d) { |
| 81 |
return getFormattedTime(d, DateFormat.LONG); |
| 82 |
} |
| 83 |
|
| 84 |
public static String getMediumFormattedTime(Date d) { |
| 85 |
return getFormattedTime(d, DateFormat.MEDIUM); |
| 86 |
} |
| 87 |
|
| 88 |
public static String getShortFormattedTime(Date d) { |
| 89 |
return getFormattedTime(d, DateFormat.SHORT); |
| 90 |
} |
| 91 |
|
| 92 |
public static String getFormattedTime(Date d, int timeStyle) { |
| 93 |
UIViewRoot viewRoot = FacesContext.getCurrentInstance().getViewRoot(); |
| 94 |
Locale locale = null; |
| 95 |
if (viewRoot != null) { |
| 96 |
locale = viewRoot.getLocale(); |
| 97 |
} else { |
| 98 |
locale = Locale.ENGLISH; |
| 99 |
} |
| 100 |
|
| 101 |
DateFormat df = DateFormat.getTimeInstance(timeStyle, locale); |
| 102 |
return df.format(d); |
| 103 |
} |
| 104 |
} |