1 /**
2 * Copyright 2010-2012 The Kuali Foundation
3 *
4 * Licensed under the Educational Community License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.opensource.org/licenses/ecl2.php
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.kuali.common.util;
17
18 import java.util.Date;
19
20 public class FormatUtils {
21
22 protected static SimpleFormatter SF = new SimpleFormatter();
23
24 /**
25 * Parse a date from the string. The string must be in the same format returned by the getDate() methods
26 */
27 public static Date parseDate(String date) {
28 return SF.parseDate(date);
29 }
30
31 /**
32 * Return a formatted date
33 */
34 public static String getDate(long millis) {
35 return SF.getDate(millis);
36 }
37
38 /**
39 * Return a formatted date
40 */
41 public static String getDate(Date date) {
42 return SF.getDate(date);
43 }
44
45 /**
46 * Given a number of bytes and the number of milliseconds it took to transfer that number of bytes, return bytes/s, KB/s, MB/s, GB/s,
47 * TB/s, PB/s, or EB/s as appropriate
48 */
49 public static String getRate(long millis, long bytes) {
50 return SF.getRate(millis, bytes);
51 }
52
53 /**
54 * Return a formatted <code>count</code>
55 */
56 public static String getCount(long count) {
57 return SF.getCount(count);
58 }
59
60 /**
61 * Given milliseconds, return milliseconds, seconds, minutes, hours, days, years, decades, or centuries as appropriate. Note that years,
62 * decades, and centuries are approximations since the logic always assumes there are exactly 365 days per year.
63 */
64 public static String getTime(long millis) {
65 return SF.getTime(millis);
66 }
67
68 /**
69 * Given a number of bytes return bytes, kilobytes, megabytes, gigabytes, terabytes, petabytes, or exabytes as appropriate.
70 */
71 public static String getSize(long bytes) {
72 return SF.getSize(bytes);
73 }
74
75 /**
76 * Given a number of bytes return a string formatted into the unit of measure indicated
77 */
78 public static String getSize(long bytes, Size unitOfMeasure) {
79 return SF.getSize(bytes, unitOfMeasure);
80 }
81
82 public static String getFormattedSize(long bytes, Size size) {
83 return SF.getFormattedSize(bytes, size);
84 }
85
86 public static Size getSizeEnum(double bytes) {
87 return SF.getSizeEnum(bytes);
88 }
89
90 }