01: /*
02: * $Id: DateUtils.java 8077 2007-08-27 20:15:25Z aperepel $
03: * --------------------------------------------------------------------------------------
04: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
05: *
06: * The software in this package is published under the terms of the CPAL v1.0
07: * license, a copy of which has been included with this distribution in the
08: * LICENSE.txt file.
09: */
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: public static String getTimeStamp(String format) {
27: // Format the current time.
28: SimpleDateFormat formatter = new SimpleDateFormat(format);
29: Date currentTime = new Date();
30: return formatter.format(currentTime);
31: }
32:
33: public static String formatTimeStamp(Date dateTime, String format) {
34: // Format the current time.
35: SimpleDateFormat formatter = new SimpleDateFormat(format);
36: return formatter.format(dateTime);
37: }
38:
39: public static String getStringFromDate(Date date, String format) {
40: // converts from date to strin using the standard TIME_STAMP_FORMAT
41: // pattern
42: SimpleDateFormat formatter = new SimpleDateFormat(format);
43: return formatter.format(date);
44: }
45:
46: public static Date getDateFromString(String date, String format) {
47: // The date must always be in the format of TIME_STAMP_FORMAT
48: // i.e. JAN 29 2001 22:50:40 GMT
49: SimpleDateFormat formatter = new SimpleDateFormat(format);
50: ParsePosition pos = new ParsePosition(0);
51:
52: // Parse the string back into a Time Stamp.
53: return formatter.parse(date, pos);
54: }
55:
56: public static String getFormattedDuration(long mills) {
57: long days = mills / 86400000;
58: mills = mills - (days * 86400000);
59: long hours = mills / 3600000;
60: mills = mills - (hours * 3600000);
61: long mins = mills / 60000;
62: mills = mills - (mins * 60000);
63: long secs = mills / 1000;
64: mills = mills - (secs * 1000);
65:
66: StringBuffer bf = new StringBuffer(60);
67: bf.append(days).append(" ").append(
68: CoreMessages.days().getMessage()).append(", ");
69: bf.append(hours).append(" ").append(
70: CoreMessages.hours().getMessage()).append(", ");
71: bf.append(mins).append(" ").append(
72: CoreMessages.minutes().getMessage()).append(", ");
73: bf.append(secs).append(".").append(mills).append(" ").append(
74: CoreMessages.seconds().getMessage());
75: return bf.toString();
76: }
77:
78: }
|