View Javadoc

1   /*
2    * $Id: DateUtils.java 7963 2007-08-21 08:53:15Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.util;
12  
13  import org.mule.config.i18n.CoreMessages;
14  
15  import java.text.ParsePosition;
16  import java.text.SimpleDateFormat;
17  import java.util.Date;
18  
19  /**
20   * <code>DateUtils</code> contains some useful methods dealing date/time
21   * conversion, formatting etc.
22   */
23  // @ThreadSafe
24  public class DateUtils extends org.apache.commons.lang.time.DateUtils
25  {
26  
27      public static String getTimeStamp(String format)
28      {
29          // Format the current time.
30          SimpleDateFormat formatter = new SimpleDateFormat(format);
31          Date currentTime = new Date();
32          return formatter.format(currentTime);
33      }
34  
35      public static String formatTimeStamp(Date dateTime, String format)
36      {
37          // Format the current time.
38          SimpleDateFormat formatter = new SimpleDateFormat(format);
39          return formatter.format(dateTime);
40      }
41  
42      public static String getStringFromDate(Date date, String format)
43      {
44          // converts from date to strin using the standard TIME_STAMP_FORMAT
45          // pattern
46          SimpleDateFormat formatter = new SimpleDateFormat(format);
47          return formatter.format(date);
48      }
49  
50      public static Date getDateFromString(String date, String format)
51      {
52          // The date must always be in the format of TIME_STAMP_FORMAT
53          // i.e. JAN 29 2001 22:50:40 GMT
54          SimpleDateFormat formatter = new SimpleDateFormat(format);
55          ParsePosition pos = new ParsePosition(0);
56  
57          // Parse the string back into a Time Stamp.
58          return formatter.parse(date, pos);
59      }
60  
61      public static String getFormattedDuration(long mills)
62      {
63          long days = mills / 86400000;
64          mills = mills - (days * 86400000);
65          long hours = mills / 3600000;
66          mills = mills - (hours * 3600000);
67          long mins = mills / 60000;
68          mills = mills - (mins * 60000);
69          long secs = mills / 1000;
70          mills = mills - (secs * 1000);
71  
72          StringBuffer bf = new StringBuffer(60);
73          bf.append(days).append(" ").append(CoreMessages.days().getMessage()).append(", ");
74          bf.append(hours).append(" ").append(CoreMessages.hours().getMessage()).append(", ");
75          bf.append(mins).append(" ").append(CoreMessages.minutes().getMessage()).append(", ");
76          bf.append(secs).append(".").append(mills).append(" ").append(CoreMessages.seconds().getMessage());
77          return bf.toString();
78      }
79  
80  }