| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| DateUtil |
|
| 5.8;5.8 |
| 1 | /****************************************************************************** | |
| 2 | * DateUtil.java - Date utilities | |
| 3 | * | |
| 4 | * PicMan - The BuckoSoft Picture Manager in Java | |
| 5 | * Copyright(c) 2007 - Dick Balaska | |
| 6 | * | |
| 7 | */ | |
| 8 | package com.buckosoft.PicMan.util; | |
| 9 | ||
| 10 | import java.text.DateFormat; | |
| 11 | import java.util.Date; | |
| 12 | ||
| 13 | /** Date utilities. | |
| 14 | * @author Dick Balaska | |
| 15 | * @since 2007/12/07 | |
| 16 | * @see <a href="http://cvs.buckosoft.com/Projects/java/PicMan/PicMan/src/com/buckosoft/PicMan/util/DateUtil.java">DateUtil.java</a> | |
| 17 | */ | |
| 18 | public class DateUtil { | |
| 19 | 0 | protected DateUtil() {} // Not an object that gets instantiated |
| 20 | ||
| 21 | public static final String s_colon = ":"; | |
| 22 | ||
| 23 | /////////////////////////////////////////////////////////////////////////////// | |
| 24 | /** Return the time passed as a string | |
| 25 | * @param then The date to compare to | |
| 26 | * @return "3 hours" | |
| 27 | */ | |
| 28 | public static String getTimeRelative(Date then) { | |
| 29 | 0 | return(getTimeRelative(then, false)); |
| 30 | } | |
| 31 | ||
| 32 | public static String getTimeRelative(Date then, boolean past) { | |
| 33 | 0 | String ago = " ago"; |
| 34 | 0 | if (past) |
| 35 | 0 | ago = ""; |
| 36 | 0 | StringBuffer sb = new StringBuffer(); |
| 37 | ||
| 38 | 0 | Date rightnow = new Date(); |
| 39 | 0 | long diff = (rightnow.getTime() - then.getTime())/1000; |
| 40 | 0 | if (diff < 0) |
| 41 | { | |
| 42 | 0 | diff = -diff; |
| 43 | 0 | ago = ""; |
| 44 | } | |
| 45 | 0 | if (diff < 60) |
| 46 | 0 | sb.append(diff + " second" + (diff == 1 ? "" : "s") + ago); |
| 47 | 0 | else if (diff < (60*90)) // 1.5 hours |
| 48 | 0 | sb.append(diff/60 + " minute" + ((diff/60 == 1) ? "" : "s") + ago); |
| 49 | 0 | else if (diff < (60*60*50)) |
| 50 | 0 | sb.append(diff/(60*60) + " hour" + (diff/(60*60) == 1 ? "" : "s") + ago); |
| 51 | 0 | else if (diff < (60*60*24*21)) |
| 52 | 0 | sb.append(diff/(60*60*24) + " day" + (diff/(60*60*24)== 1 ? "" : "s") + ago); |
| 53 | 0 | else if (diff < (60*60*24*7*5)) |
| 54 | 0 | sb.append(diff/(60*60*24*7) + " week" + (diff/(60*60*24*7) == 1 ? "" : "s") + ago); |
| 55 | else | |
| 56 | 0 | sb.append(diff/(60*60*24*30) + " month" + (diff/(60*60*24*30) == 1 ? "" : "s") + ago); |
| 57 | 0 | return(sb.toString()); |
| 58 | } | |
| 59 | ||
| 60 | 0 | private static DateFormat df = DateFormat.getTimeInstance(DateFormat.SHORT); |
| 61 | public static String getTimeHMS(Date when) { | |
| 62 | 0 | return(df.format(when)); |
| 63 | } | |
| 64 | ||
| 65 | /** Convert a long to a String like dd:hh:mm:ss. | |
| 66 | * @param time Usually a diff between currentTime.getTime() and startTime.getTime(). Includes milliseconds. | |
| 67 | * @return A String | |
| 68 | */ | |
| 69 | public static String timeToString(long time) { | |
| 70 | 0 | StringBuilder sb = new StringBuilder(); |
| 71 | 0 | time = time /1000; // discard milliseconds |
| 72 | 0 | int s = (int)(time % 60); |
| 73 | 0 | int m = (int)((time / 60) % 60); |
| 74 | 0 | int h = (int)((time / (60*60)) % (24)); |
| 75 | 0 | int d = (int)((time / (60*60*24))); |
| 76 | 0 | if (s < 0 || m < 0 || h < 0 || d < 0) |
| 77 | 0 | sb.append("-"); |
| 78 | 0 | s = Math.abs(s); |
| 79 | 0 | m = Math.abs(m); |
| 80 | 0 | h = Math.abs(h); |
| 81 | 0 | d = Math.abs(d); |
| 82 | 0 | if (d > 0) { |
| 83 | 0 | sb.append(d); |
| 84 | 0 | sb.append(":"); |
| 85 | } | |
| 86 | 0 | if (h < 10 && d > 0) |
| 87 | 0 | sb.append("0"); |
| 88 | 0 | if (h > 0 || d > 0) { |
| 89 | 0 | sb.append(h); |
| 90 | 0 | sb.append(":"); |
| 91 | } | |
| 92 | 0 | if (m < 10) |
| 93 | 0 | sb.append("0"); |
| 94 | 0 | sb.append(m); |
| 95 | 0 | sb.append(":"); |
| 96 | 0 | if (s < 10) |
| 97 | 0 | sb.append("0"); |
| 98 | 0 | sb.append(s); |
| 99 | 0 | return(sb.toString()); |
| 100 | } | |
| 101 | ||
| 102 | ||
| 103 | } |