Source code: org/mitre/cvw/DateUtils.java
1 /*
2 * Copyright (c) 1996-2000. The MITRE Corporation (http://www.mitre.org/).
3 * All rights reserved.
4 * CVW comes with ABSOLUTELY NO WARRANTY. See license for details.
5 */
6
7 package org.mitre.cvw;
8
9 import java.text.SimpleDateFormat;
10 import java.util.TimeZone;
11 import java.util.Date;
12
13 /**
14 * This class provides static functions for displaying dates
15 * @author Deb Ercolini, The MITRE Corporation
16 * @version 1
17 */
18
19 public class DateUtils extends Object {
20
21 public static final Date dateZero = new Date(convertDateFromServer("0"));
22
23 /**
24 * Returns a string representing the date based on the user preference
25 * for local time or GMT time.
26 * @param the date to be displayed
27 * @return the display string for the date passed in
28 */
29 public static String formatDateTime(Date date) {
30 return formatDateUsingPattern(date, "EEE MMM dd HH:mm:ss yyyy zzz");
31 }
32
33 /**
34 * Returns a string representing the server date string based on the
35 * user preference for local time or GMT time.
36 * @param the date string from the CVW server to be displayed
37 * @return the display string for the date string passed in
38 */
39 public static String formatDateTime(String time) {
40 return formatDateTime(new Date(convertDateFromServer(time)));
41 }
42
43 /**
44 * Returns a string representing the date based on the user preference
45 * for local time or GMT time.
46 * @param the date to be displayed
47 * @return the display string for the date passed in
48 */
49 public static String formatDate(Date date) {
50 return formatDateUsingPattern(date, "dd-MMM-yy");
51 }
52
53
54 /**
55 * Returns a string representing the server date string based on the
56 * user preference for local time or GMT time.
57 * @param the date string from the CVW server to be displayed
58 * @return the display string for the date string passed in
59 */
60 public static String formatDate(String time) {
61 return formatDate(new Date(convertDateFromServer(time)));
62 }
63
64 /**
65 * Returns a string representing the date based on the user preference
66 * for local time or GMT time.
67 * @param the date to be displayed
68 * @param the pattern to be used
69 * @return the display string for the date passed in
70 */
71 public static String formatDateUsingPattern(Date date, String pat) {
72 boolean localTZ = CVWCoordinator.getInstance().useLocalTimeZone();
73 SimpleDateFormat formatter = new SimpleDateFormat(pat);
74 if (!localTZ)
75 formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
76 return formatter.format(date);
77 }
78
79 /**
80 * Returns a string representing the date string using the pattern to
81 * the parse and format as the display format based on the user preference
82 * for local time or GMT time.
83 * @param date the date to be displayed
84 * @param pat the pattern to be used to parse the date
85 * @param format the format to be used to return
86 * @return the display string for the date passed in
87 */
88 public static String parseFormatDateUsingPattern(String date, String pat, String format) {
89 try {
90 boolean localTZ = CVWCoordinator.getInstance().useLocalTimeZone();
91 SimpleDateFormat parser = new SimpleDateFormat(pat);
92 Date d = parser.parse(date);
93 SimpleDateFormat formatter = new SimpleDateFormat(format);
94 if (!localTZ)
95 formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
96 return formatter.format(d);
97 } catch (Exception e) {
98 System.err.println("parse date Exception: " + e);
99 return date;
100 }
101 }
102
103
104 /**
105 * Returns a long value representing the date given a string.
106 * java dates are stored in milliseconds and the CVW server measures
107 * in seconds so we append "000"
108 * Dee Goepel
109 *
110 * @param str the date from the CVW server to be converted
111 * @return a long integeter representing the date
112 */
113 public static long convertDateFromServer(String str) {
114 return (Long.parseLong(str+"000"));
115 }
116
117 }