View Javadoc
1 /*** 2 * Copyright (c) 2002, CodeStreet LLC. All rights reserved.<p> 3 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following 4 * conditions are met:<p> 5 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 6 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer 7 * in the documentation and/or other materials provided with the distribution. Neither the name of CodeStreet LLC. nor the 8 * names of its contributors may be used to endorse or promote products derived from this software without specific prior written 9 * permission.<p> 10 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 11 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 12 * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 13 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 14 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 15 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.<p> 16 */ 17 18 package com.codestreet.messageforge; 19 20 import java.util.Date; 21 import java.text.SimpleDateFormat; 22 import java.util.TimeZone; 23 24 /*** 25 * Singleton class for formatting <tt>Date</tt> fields as <code <String</tt>, 26 * and creating <tt>Date</tt> fields from a <code <String</tt>. 27 * 28 * @author Jawaid Hakim. 29 */ 30 public class RDateFormat 31 { 32 /*** 33 * Ctor. 34 */ 35 protected RDateFormat() 36 { 37 } 38 39 /*** 40 * Get singleton instance. 41 * 42 * @return Singleton instance. 43 */ 44 public static RDateFormat getInstance() 45 { 46 return instance_; 47 } 48 49 /*** 50 * Format a date. 51 * 52 * @param date 53 * Date to format. 54 * @return Formatted date. 55 */ 56 public String format(Date date) 57 { 58 SimpleDateFormat dateFmt = getDateFormat(); 59 String strDate = dateFmt.format(date); 60 return strDate; 61 } 62 63 /*** 64 * Format a date. 65 * 66 * @param date 67 * Date to format. 68 * @return Formatted date. 69 */ 70 public String format(Object date) 71 { 72 return format((Date)date); 73 } 74 75 /*** 76 * Parse a string and create a date. 77 * 78 * @param strDate 79 * String date. 80 * @return Date. 81 */ 82 public Date parse(String strDate) throws java.text.ParseException 83 { 84 SimpleDateFormat dateFmt = getDateFormat(); 85 return dateFmt.parse(strDate); 86 } 87 88 private static SimpleDateFormat getDateFormat() 89 { 90 SimpleDateFormat dateFmt = (SimpleDateFormat) thrdLocal_.get(); 91 if (dateFmt == null) 92 { 93 dateFmt = new SimpleDateFormat(PATTERN); 94 thrdLocal_.set(dateFmt); 95 } 96 dateFmt.setLenient(true); 97 dateFmt.setTimeZone(TimeZone.getTimeZone("GMT")); 98 return dateFmt; 99 } 100 101 /*** 102 * Set the data format pattern. The defaul date format pattern is based on 103 * the Internet Engineering Task Force (IETF) Request for Comments (RFC) 104 * 1123 and is <code>EEE, d MMM yyyy HH:mm:ss z</code>. 105 * 106 * @param pattern 107 * Date format pattern 108 */ 109 public static void setPattern(String pattern) 110 { 111 PATTERN = pattern; 112 } 113 114 /*** 115 * Get the data format pattern. The defaul date format pattern is based on 116 * the Internet Engineering Task Force (IETF) Request for Comments (RFC) 117 * 1123 and is <code>EEE, d MMM yyyy HH:mm:ss z</code>. 118 * 119 * @return Date format pattern 120 */ 121 public static String getPattern() 122 { 123 return PATTERN; 124 } 125 126 private static RDateFormat instance_ = new RDateFormat(); 127 128 private static final String DEFAULT_PATTERN = "dd MMM yyyy HH:mm:ss,SSS z"; 129 130 private static String PATTERN = DEFAULT_PATTERN; 131 132 private static ThreadLocal thrdLocal_ = new ThreadLocal(); 133 }

This page was automatically generated by Maven