Child: [r23] (diff)

Download this file

time.java    183 lines (145 with data), 5.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* Some handy routines to help on everyday tasks
*/
package utils;
import java.io.File;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
*
* @author Nuno Brito, 20th of March 2011 at Germany
*/
public class time {
/**
* Pauses the current thread for a while
* @param time_to_wait in seconds
*/
public static void wait(int time_to_wait){
try {
Thread.sleep(time_to_wait * 1000);
}
catch (InterruptedException ex) {
}
}
/**
* Pauses the current thread for a while
* @param time_to_wait in Milliseconds
*/
public static void waitMs(long time_to_wait){
try {
Thread.sleep(time_to_wait);
}
catch (InterruptedException ex) {
}
}
/**
* Converts a given quantity of time into a human readable format
*
* @param ms Number of miliseconds to quantify
* @return A human readable result
*/
public static String timeNumberToHumanReadable(long ms){
String time;
long seconds;
long minutes;
long hours;
long days;
seconds = ms / 1000;
minutes = seconds / 60;
seconds %= 60;
hours = minutes / 60;
minutes %= 60;
days = hours / 24;
hours %= 24;
time = "";
if(days >0) time=days
+(days == 1 ? " day" : " days")+", ";
if(hours >0) time=time+hours
+(hours == 1 ? " hour" : " hours")+", ";
if(minutes >0) time=time+minutes
+(minutes == 1 ? " minute" : " minutes")+" and ";
time=time+seconds
+(seconds == 1 ? " second" : " seconds");
return time;
}
/**
* Converts a given date to a unique number
* @return
*/
public static long textDateToMilliseconds(int yearTo, int monthTo, int dayTo){
return textDateToMilliseconds(""+yearTo, ""+monthTo, ""+dayTo);
}
/** Convert from a normal date to correct milliseconds representation */
public static long textDateToMilliseconds(String yearTo, String monthTo, String dayTo){
// Calendar calendar = Calendar.getInstance();
// calendar.set(
// Integer.parseInt(yearTo),
// Integer.parseInt(monthTo),
// Integer.parseInt(dayTo)
// );
// return calendar.getTimeInMillis();
DateFormat dateFormat = new SimpleDateFormat("yy-MM-dd");
Date date;
try {
date = dateFormat.parse(
// yearTo + "-" + dayTo + "-" + monthTo
yearTo + "-" + monthTo + "-" + dayTo
);
return date.getTime();
} catch (ParseException ex) {
return -1;
}
}
/** Convert from a normal date to correct milliseconds representation */
public static Date getDate(int yearTo, int monthTo, int dayTo){
DateFormat dateFormat = new SimpleDateFormat("yy-MM-dd");
Date date;
try {
date = dateFormat.parse(
yearTo + "-" + monthTo + "-" + dayTo
);
return date;
} catch (ParseException ex) {
return null;
}
}
/** get the current time in a human readable manner */
public static String getDateTime() {
// code adapted from http://goo.gl/rZ716
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
return dateFormat.format(date);
}
/** get the current time in a human readable manner */
public static String getTimeFromLong(long time) {
// code adapted from http://goo.gl/rZ716
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
//Date date = new Date();
return dateFormat.format(time);
}
public static String getTimeFromLongNoDate(long time) {
// code adapted from http://goo.gl/rZ716
DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
//Date date = new Date();
return dateFormat.format(time);
}
/** get the current time in a human readable manner */
public static String getCurrentYear() {
// code adapted from http://goo.gl/rZ716
DateFormat dateFormat = new SimpleDateFormat("yyyy");
Date date = new Date();
return dateFormat.format(date);
}
/**
* Returns the age of a given file using days as value
* A result of "1" means today, "2" means more than 24 hours and so forth
*/
public static int getFileAge(File file){
long fileAge = System.currentTimeMillis() - file.lastModified();
DateFormat dateFormat = new SimpleDateFormat("dd");
int dateValue = Integer.parseInt(dateFormat.format(fileAge));
return dateValue;
}
}