KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > drftpd > util > Time


1 /*
2  * This file is part of DrFTPD, Distributed FTP Daemon.
3  *
4  * DrFTPD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * DrFTPD is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with DrFTPD; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package net.sf.drftpd.util;
19
20 /**
21  * @author Flowman
22  * @version $Id: Time.java,v 1.5 2004/04/20 04:11:51 mog Exp $
23  */

24 public class Time {
25     /**
26      * @return human readable string for time.
27      */

28     public static String JavaDoc formatTime(long millis) {
29         long days = 0, hours = 0, mins = 0, secs = 0;
30         String JavaDoc time = "";
31  
32         secs = millis / 1000;
33  
34         while ( secs >= 86400) {
35             days++;
36             secs -= 86400;
37         }
38         while ( secs >= 3600 ) {
39             hours++;
40             secs -= 3600;
41         }
42         while ( secs >= 60 ) {
43             mins++;
44             secs -= 60;
45         }
46         if ( days != 0 ) time = days + "days ";
47         if ( hours != 0 ) time = hours + "h ";
48         if ( mins != 0 ) time += mins + "m ";
49         time += secs + "s";
50         
51         return time;
52     }
53     public static long parseTime(String JavaDoc s) {
54         s=s.toLowerCase();
55         if(s.endsWith("ms")) {
56             return Long.parseLong(s.substring(0, s.length()-2));
57         }
58         if (s.endsWith("s")) {
59             return Long.parseLong(s.substring(0, s.length()-1))*1000;
60         }
61         if(s.endsWith("m")) {
62             return Long.parseLong(s.substring(0, s.length()-1))*60000;
63         }
64         return Long.parseLong(s);
65     }
66 }
Popular Tags