|
|||||||||||||||||||
| 30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| DateUtils.java | - | 29.3% | 27.8% | 28.8% |
|
||||||||||||||
| 1 |
/*
|
|
| 2 |
* Joey and its relative products are published under the terms
|
|
| 3 |
* of the Apache Software License.
|
|
| 4 |
*/
|
|
| 5 |
package org.asyrinx.brownie.core.util;
|
|
| 6 |
|
|
| 7 |
import java.util.Calendar;
|
|
| 8 |
import java.util.Date;
|
|
| 9 |
import java.util.GregorianCalendar;
|
|
| 10 |
|
|
| 11 |
/**
|
|
| 12 |
* @author akima
|
|
| 13 |
*/
|
|
| 14 |
public final class DateUtils { |
|
| 15 |
|
|
| 16 | 0 |
public static Date toDateTime(int year, int month, int day, int hours, |
| 17 |
int minuites, int seconds, int milliseconds) { |
|
| 18 | 0 |
final Calendar cal = Calendar.getInstance(); |
| 19 | 0 |
cal.set(year, month, day, hours, minuites, seconds); |
| 20 | 0 |
cal.set(Calendar.MILLISECOND, milliseconds); |
| 21 | 0 |
return new Date(cal.getTime().getTime()); |
| 22 |
} |
|
| 23 |
|
|
| 24 | 0 |
public static Date adjustDateTime(Date d, int hours, int minuites, |
| 25 |
int seconds, int miliseconds) { |
|
| 26 | 0 |
final Calendar cal = Calendar.getInstance(); |
| 27 | 0 |
cal.setTime(d); |
| 28 | 0 |
return toDateTime(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal
|
| 29 |
.get(Calendar.DAY_OF_MONTH), hours, minuites, seconds, |
|
| 30 |
miliseconds); |
|
| 31 |
} |
|
| 32 |
|
|
| 33 | 0 |
public static Date adjustDateBegin(Date d) { |
| 34 | 0 |
return adjustDateTime(d, 0, 0, 0, 0);
|
| 35 |
} |
|
| 36 |
|
|
| 37 | 0 |
public static Date adjustDateEnd(Date d) { |
| 38 | 0 |
return adjustDateTime(d, 23, 59, 59, 999);
|
| 39 |
} |
|
| 40 |
|
|
| 41 | 0 |
public static int get(Date d, int calendarField) { |
| 42 | 0 |
final Calendar cal = Calendar.getInstance(); |
| 43 | 0 |
cal.setTime(d); |
| 44 | 0 |
return cal.get(calendarField);
|
| 45 |
} |
|
| 46 |
|
|
| 47 | 0 |
public static int getYear(Date d) { |
| 48 | 0 |
return get(d, Calendar.YEAR);
|
| 49 |
} |
|
| 50 |
|
|
| 51 | 0 |
public static int getMonth(Date d) { |
| 52 | 0 |
return get(d, Calendar.MONTH);
|
| 53 |
} |
|
| 54 |
|
|
| 55 | 0 |
public static int getDay(Date d) { |
| 56 | 0 |
return get(d, Calendar.DAY_OF_MONTH);
|
| 57 |
} |
|
| 58 |
|
|
| 59 |
/**
|
|
| 60 |
* 指定された年月日からDateオブジェクトを生成します。 <br>
|
|
| 61 |
* 月は1オリジンです。 <br>
|
|
| 62 |
*
|
|
| 63 |
* @param year
|
|
| 64 |
* 年
|
|
| 65 |
* @param month
|
|
| 66 |
* 月(1-12)
|
|
| 67 |
* @param day
|
|
| 68 |
* 日
|
|
| 69 |
* @return
|
|
| 70 |
*/
|
|
| 71 | 1 |
public static Date toDate(int year, int month, int day) { |
| 72 | 1 |
return toDate(year, month, day, 0, 0);
|
| 73 |
} |
|
| 74 |
|
|
| 75 |
/**
|
|
| 76 |
* 指定された年月日時分からDateオブジェクトを生成します。 <br>
|
|
| 77 |
* 月は1オリジンです。 <br>
|
|
| 78 |
*
|
|
| 79 |
* @param year
|
|
| 80 |
* 年
|
|
| 81 |
* @param month
|
|
| 82 |
* 月(1-12)
|
|
| 83 |
* @param day
|
|
| 84 |
* 日
|
|
| 85 |
* @param hours
|
|
| 86 |
* 時(0-23)
|
|
| 87 |
* @param minutes
|
|
| 88 |
* 分(0-59)
|
|
| 89 |
* @return
|
|
| 90 |
*/
|
|
| 91 | 3 |
public static Date toDate(int year, int month, int day, int hours, |
| 92 |
int minutes) {
|
|
| 93 | 3 |
return toDate(year, month, day, hours, minutes, 0);
|
| 94 |
} |
|
| 95 |
|
|
| 96 |
/**
|
|
| 97 |
* 指定された年月日時分秒からDateオブジェクトを生成します。 <br>
|
|
| 98 |
* 月は1オリジンです。 <br>
|
|
| 99 |
*
|
|
| 100 |
* @param year
|
|
| 101 |
* 年
|
|
| 102 |
* @param month
|
|
| 103 |
* 月(1-12)
|
|
| 104 |
* @param day
|
|
| 105 |
* 日
|
|
| 106 |
* @param hours
|
|
| 107 |
* 時(0-23)
|
|
| 108 |
* @param minutes
|
|
| 109 |
* 分(0-59)
|
|
| 110 |
* @param seconds
|
|
| 111 |
* 秒(0-59)
|
|
| 112 |
* @return
|
|
| 113 |
*/
|
|
| 114 | 5 |
public static Date toDate(int year, int month, int day, int hours, |
| 115 |
int minutes, int seconds) { |
|
| 116 | 5 |
return toDate(year, month, day, hours, minutes, seconds, 0);
|
| 117 |
} |
|
| 118 |
|
|
| 119 |
/**
|
|
| 120 |
* 指定された年月日時分秒ミリ秒からDateオブジェクトを生成します。 <br>
|
|
| 121 |
* 月は1オリジンです。 <br>
|
|
| 122 |
*
|
|
| 123 |
* @param year
|
|
| 124 |
* 年
|
|
| 125 |
* @param month
|
|
| 126 |
* 月(1-12)
|
|
| 127 |
* @param day
|
|
| 128 |
* 日
|
|
| 129 |
* @param hours
|
|
| 130 |
* 時(0-23)
|
|
| 131 |
* @param minutes
|
|
| 132 |
* 分(0-59)
|
|
| 133 |
* @param seconds
|
|
| 134 |
* 秒(0-59)
|
|
| 135 |
* @param milliseconds
|
|
| 136 |
* ミリ秒(0-999)
|
|
| 137 |
* @return
|
|
| 138 |
*/
|
|
| 139 | 13 |
public static Date toDate(int year, int month, int day, int hours, |
| 140 |
int minutes, int seconds, int milliseconds) { |
|
| 141 | 13 |
final Calendar cal = Calendar.getInstance(); |
| 142 | 13 |
cal.set(year, month - 1, day, hours, minutes, seconds); |
| 143 | 13 |
cal.set(Calendar.MILLISECOND, milliseconds); |
| 144 | 13 |
return new Date(cal.getTime().getTime()); |
| 145 |
} |
|
| 146 |
|
|
| 147 |
/**
|
|
| 148 |
* 指定された日付の0時0分0秒0ミリ秒に設定されたDateオブジェクトを返す
|
|
| 149 |
*
|
|
| 150 |
* @param y
|
|
| 151 |
* @param m
|
|
| 152 |
* @param d
|
|
| 153 |
* @return
|
|
| 154 |
*/
|
|
| 155 | 0 |
public static Date toDateBegin(int y, int m, int d) { |
| 156 | 0 |
return toDateTime(y, m, d, 0, 0, 0, 0);
|
| 157 |
} |
|
| 158 |
|
|
| 159 |
/**
|
|
| 160 |
* 指定された日付の23時59分59秒999ミリ秒に設定されたDateオブジェクトを返す
|
|
| 161 |
*
|
|
| 162 |
* @param y
|
|
| 163 |
* @param m
|
|
| 164 |
* @param d
|
|
| 165 |
* @return
|
|
| 166 |
*/
|
|
| 167 | 0 |
public static Date toDateEnd(int y, int m, int d) { |
| 168 | 0 |
return toDateTime(y, m, d, 23, 59, 59, 999);
|
| 169 |
} |
|
| 170 |
|
|
| 171 |
/**
|
|
| 172 |
* うるう年かどうかを判断します。
|
|
| 173 |
*
|
|
| 174 |
* @param year
|
|
| 175 |
* @return
|
|
| 176 |
*/
|
|
| 177 | 0 |
public static boolean isLeapYear(int year) { |
| 178 | 0 |
final GregorianCalendar calendar = new GregorianCalendar();
|
| 179 | 0 |
return calendar.isLeapYear(year);
|
| 180 |
} |
|
| 181 |
|
|
| 182 |
/**
|
|
| 183 |
* 指定された月の最初の日付をDate型で返します。
|
|
| 184 |
*
|
|
| 185 |
* @param year
|
|
| 186 |
* 年
|
|
| 187 |
* @param month
|
|
| 188 |
* 月 1オリジンです
|
|
| 189 |
* @return
|
|
| 190 |
*/
|
|
| 191 | 0 |
public static Date getBeginDateOfMonth(int year, int month) { |
| 192 | 0 |
final Calendar calendar = Calendar.getInstance(); |
| 193 | 0 |
calendar.set(Calendar.YEAR, year); |
| 194 | 0 |
calendar.set(Calendar.MONTH, month - 1); |
| 195 | 0 |
calendar.set(Calendar.DAY_OF_MONTH, 1); |
| 196 | 0 |
return new Date(calendar.getTimeInMillis()); |
| 197 |
} |
|
| 198 |
|
|
| 199 |
/**
|
|
| 200 |
* 指定された月の最後の日付をDate型で返します。
|
|
| 201 |
*
|
|
| 202 |
* @param year
|
|
| 203 |
* 年
|
|
| 204 |
* @param month
|
|
| 205 |
* 月 1オリジンです
|
|
| 206 |
* @return
|
|
| 207 |
*/
|
|
| 208 | 0 |
public static Date getLastDateOfMonth(int year, int month) { |
| 209 | 0 |
final Calendar calendar = Calendar.getInstance(); |
| 210 | 0 |
calendar.set(Calendar.YEAR, year); |
| 211 | 0 |
calendar.set(Calendar.MONTH, month - 1); |
| 212 | 0 |
calendar.set(Calendar.DAY_OF_MONTH, getLastDayOfMonth(year, month)); |
| 213 | 0 |
return new Date(calendar.getTimeInMillis()); |
| 214 |
} |
|
| 215 |
|
|
| 216 |
/**
|
|
| 217 |
* 指定された月の最後の日をintで返します。
|
|
| 218 |
*
|
|
| 219 |
* @param year
|
|
| 220 |
* 年
|
|
| 221 |
* @param month
|
|
| 222 |
* 月 1オリジンです
|
|
| 223 |
* @return
|
|
| 224 |
*/
|
|
| 225 | 24 |
public static int getLastDayOfMonth(int year, int month) { |
| 226 | 24 |
GregorianCalendar calendar = new GregorianCalendar();
|
| 227 | 24 |
calendar.set(Calendar.YEAR, year); |
| 228 | 24 |
calendar.set(Calendar.MONTH, month - 1); |
| 229 | 24 |
calendar.set(Calendar.DAY_OF_MONTH, 1); //これがないとダメです。
|
| 230 | 24 |
return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
|
| 231 |
} |
|
| 232 |
|
|
| 233 |
} |
|
||||||||||