KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > util > DateUtils


1 /*
2  * $Id: DateUtils.java 3798 2006-11-04 04:07:14Z aperepel $
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 MuleSource MPL
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 java.text.ParsePosition JavaDoc;
14 import java.text.SimpleDateFormat JavaDoc;
15 import java.util.Date JavaDoc;
16
17 import org.mule.config.i18n.Message;
18 import org.mule.config.i18n.Messages;
19
20 /**
21  * <code>DateUtils</code> contains some useful methods dealing date/time
22  * conversion, formatting etc.
23  */

24 // @ThreadSafe
25
public class DateUtils extends org.apache.commons.lang.time.DateUtils
26 {
27
28     public static String JavaDoc getTimeStamp(String JavaDoc format)
29     {
30         // Format the current time.
31
SimpleDateFormat JavaDoc formatter = new SimpleDateFormat JavaDoc(format);
32         Date JavaDoc currentTime = new Date JavaDoc();
33         return formatter.format(currentTime);
34     }
35
36     public static String JavaDoc formatTimeStamp(Date JavaDoc dateTime, String JavaDoc format)
37     {
38         // Format the current time.
39
SimpleDateFormat JavaDoc formatter = new SimpleDateFormat JavaDoc(format);
40         return formatter.format(dateTime);
41     }
42
43     public static String JavaDoc getStringFromDate(Date JavaDoc date, String JavaDoc format)
44     {
45         // converts from date to strin using the standard TIME_STAMP_FORMAT
46
// pattern
47
SimpleDateFormat JavaDoc formatter = new SimpleDateFormat JavaDoc(format);
48         return formatter.format(date);
49     }
50
51     public static Date JavaDoc getDateFromString(String JavaDoc date, String JavaDoc format)
52     {
53         // The date must always be in the format of TIME_STAMP_FORMAT
54
// i.e. JAN 29 2001 22:50:40 GMT
55
SimpleDateFormat JavaDoc formatter = new SimpleDateFormat JavaDoc(format);
56         ParsePosition JavaDoc pos = new ParsePosition JavaDoc(0);
57
58         // Parse the string back into a Time Stamp.
59
return formatter.parse(date, pos);
60     }
61
62     public static String JavaDoc getFormattedDuration(long mills)
63     {
64         long days = mills / 86400000;
65         mills = mills - (days * 86400000);
66         long hours = mills / 3600000;
67         mills = mills - (hours * 3600000);
68         long mins = mills / 60000;
69         mills = mills - (mins * 60000);
70         long secs = mills / 1000;
71         mills = mills - (secs * 1000);
72
73         StringBuffer JavaDoc bf = new StringBuffer JavaDoc(60);
74         bf.append(days).append(" ").append(new Message(Messages.DAYS).getMessage()).append(", ");
75         bf.append(hours).append(" ").append(new Message(Messages.HOURS).getMessage()).append(", ");
76         bf.append(mins).append(" ").append(new Message(Messages.MINS).getMessage()).append(", ");
77         bf.append(secs).append(".").append(mills).append(" ").append(new Message(Messages.SEC).getMessage());
78         return bf.toString();
79     }
80
81 }
82
Popular Tags