| 1 |
/*======================================================================*//** |
|---|
| 2 |
* |
|---|
| 3 |
* Utils for ActionScript 3.0 |
|---|
| 4 |
* |
|---|
| 5 |
* @author Copyright (c) 2007 Spark project. |
|---|
| 6 |
* @version 1.0.0 |
|---|
| 7 |
* |
|---|
| 8 |
* @see http://utils.libspark.org/ |
|---|
| 9 |
* @see http://www.libspark.org/ |
|---|
| 10 |
* |
|---|
| 11 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
|---|
| 12 |
* you may not use this file except in compliance with the License. |
|---|
| 13 |
* You may obtain a copy of the License at |
|---|
| 14 |
* |
|---|
| 15 |
* http://www.apache.org/licenses/LICENSE-2.0 |
|---|
| 16 |
* |
|---|
| 17 |
* Unless required by applicable law or agreed to in writing, software |
|---|
| 18 |
* distributed under the License is distributed on an "AS IS" BASIS, |
|---|
| 19 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, |
|---|
| 20 |
* either express or implied. See the License for the specific language |
|---|
| 21 |
* governing permissions and limitations under the License. |
|---|
| 22 |
* |
|---|
| 23 |
*//*=======================================================================*/ |
|---|
| 24 |
package org.libspark.utils { |
|---|
| 25 |
import flash.errors.IllegalOperationError; |
|---|
| 26 |
|
|---|
| 27 |
/** |
|---|
| 28 |
* Date クラスのためのユーティリティクラスです |
|---|
| 29 |
*/ |
|---|
| 30 |
public class DateUtil { |
|---|
| 31 |
|
|---|
| 32 |
/*======================================================================*//** |
|---|
| 33 |
* @private |
|---|
| 34 |
*//*=======================================================================*/ |
|---|
| 35 |
public function DateUtil() { |
|---|
| 36 |
throw new IllegalOperationError( "DateUtil クラスはインスタンスを生成できません。" ); |
|---|
| 37 |
} |
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
/*======================================================================*//** |
|---|
| 44 |
* 対象となる Date オブジェクトの月の最大日数を返します。 |
|---|
| 45 |
* @author taka:nium |
|---|
| 46 |
* @param date 最大日数を取得したい Date オブジェクトです。 |
|---|
| 47 |
* @return 最大日数です。 |
|---|
| 48 |
*//*=======================================================================*/ |
|---|
| 49 |
static public function getMaxDateLength( date:Date ):int { |
|---|
| 50 |
var newdate:Date = new Date( date ); |
|---|
| 51 |
newdate.setMonth( date.getMonth() + 1 ); |
|---|
| 52 |
newdate.setDate( 0 ); |
|---|
| 53 |
return newdate.getDate(); |
|---|
| 54 |
} |
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
/** |
|---|
| 58 |
* 指定の月の最初の曜日を調べます。 |
|---|
| 59 |
* |
|---|
| 60 |
* @param fullyear 西暦 |
|---|
| 61 |
* @param month 月 |
|---|
| 62 |
* @author michi at seyself.com |
|---|
| 63 |
*/ |
|---|
| 64 |
public static function getFirstDay(fullyear:uint,month:uint):uint |
|---|
| 65 |
{ |
|---|
| 66 |
var tmp:Date = new Date(fullyear,month,1); |
|---|
| 67 |
return tmp.getDay(); |
|---|
| 68 |
} |
|---|
| 69 |
|
|---|
| 70 |
/** |
|---|
| 71 |
* うるう年かどうかを調べます. |
|---|
| 72 |
* |
|---|
| 73 |
* @param fullyear 西暦 |
|---|
| 74 |
* @return うるう年の場合はtrueを返します。 |
|---|
| 75 |
* @author michi at seyself.com |
|---|
| 76 |
*/ |
|---|
| 77 |
public static function isLeap( fullyear:uint ):Boolean |
|---|
| 78 |
{ |
|---|
| 79 |
var flag:Boolean = false; |
|---|
| 80 |
if(((fullyear%4==0) && (fullyear%100!=0)) || (fullyear%400==0)) flag = true; |
|---|
| 81 |
return flag; |
|---|
| 82 |
} |
|---|
| 83 |
|
|---|
| 84 |
/** |
|---|
| 85 |
* 西暦を和暦に変換します. |
|---|
| 86 |
* 対応しているのは明治以降になります(1868年以降) |
|---|
| 87 |
* |
|---|
| 88 |
* @param fullyear 西暦 |
|---|
| 89 |
* @return 和暦(例:H20) |
|---|
| 90 |
* @author michi at seyself.com |
|---|
| 91 |
*/ |
|---|
| 92 |
public static function convertJCalendar( fullyear:uint ):String |
|---|
| 93 |
{ |
|---|
| 94 |
var gengou:Array = ["M", "T", "S", "H"]; |
|---|
| 95 |
var changeYear:Array = [1868, 1912, 1926, 1989]; |
|---|
| 96 |
var str:String = "" , reki:uint = 0; |
|---|
| 97 |
var len:uint = changeYear.length; |
|---|
| 98 |
for (var i:int = len - 1; i > 0; i--) { |
|---|
| 99 |
if (fullyear >= changeYear[i]) { |
|---|
| 100 |
str = gengou[i]; reki = fullyear - changeYear[i] + 1; |
|---|
| 101 |
break; |
|---|
| 102 |
} |
|---|
| 103 |
} |
|---|
| 104 |
if (str) { |
|---|
| 105 |
return str + reki; |
|---|
| 106 |
} |
|---|
| 107 |
return "" + fullyear; |
|---|
| 108 |
} |
|---|
| 109 |
|
|---|
| 110 |
/** |
|---|
| 111 |
* 曜日名を取得します. <br /> |
|---|
| 112 |
* type の指定によって得られる値<br /> |
|---|
| 113 |
* 0 : SUNDAY, MONDAY, TUSEDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY<br /> |
|---|
| 114 |
* 1 : Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday<br /> |
|---|
| 115 |
* 2 : sunday, monday, tuesday, wednesday, thursday, friday, saturday<br /> |
|---|
| 116 |
* 3 : SUN, MON, TUE, WED, THU, FRI, SAT<br /> |
|---|
| 117 |
* 4 : Sun, Mon, Tue, Wed, Thu, Fri, Sat<br /> |
|---|
| 118 |
* 5 : sun, mon, tue, wed, thu, fri, sat<br /> |
|---|
| 119 |
* 6 : 日曜日, 月曜日, 火曜日, 水曜日, 木曜日, 金曜日, 土曜日<br /> |
|---|
| 120 |
* 7 : 日, 月, 火, 水, 木, 金, 土<br /> |
|---|
| 121 |
* 8 : 日曜, 月曜, 火曜, 水曜, 木曜, 金曜, 土曜<br /> |
|---|
| 122 |
* |
|---|
| 123 |
* @param index 曜日インデックス値 |
|---|
| 124 |
* @param type 取得する曜日名のタイプ(0から8まで) |
|---|
| 125 |
* @return 曜日名 |
|---|
| 126 |
* @author michi at seyself.com |
|---|
| 127 |
*/ |
|---|
| 128 |
public static function getWeekName( index:uint , type:uint=0 ):String |
|---|
| 129 |
{ |
|---|
| 130 |
var week:Array; |
|---|
| 131 |
if(type==0) week = ["SUNDAY","MONDAY","TUSEDAY","WEDNESDAY","THURSDAY","FRIDAY","SATURDAY"]; |
|---|
| 132 |
else if(type==1) week = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]; |
|---|
| 133 |
else if(type==2) week = ["sunday","monday","tuesday","wednesday","thursday","friday","saturday"]; |
|---|
| 134 |
else if(type==3) week = ["SUN","MON","TUE","WED","THU","FRI","SAT"]; |
|---|
| 135 |
else if(type==4) week = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"]; |
|---|
| 136 |
else if(type==5) week = ["sun","mon","tue","wed","thu","fri","sat"]; |
|---|
| 137 |
else if(type==6) week = ["日曜日","月曜日","火曜日","水曜日","木曜日","金曜日","土曜日"]; |
|---|
| 138 |
else if(type==7) week = ["日","月","火","水","木","金","土"]; |
|---|
| 139 |
else if(type==8) week = ["日曜","月曜","火曜","水曜","木曜","金曜","土曜"]; |
|---|
| 140 |
else week = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"]; |
|---|
| 141 |
return week[index]; |
|---|
| 142 |
} |
|---|
| 143 |
|
|---|
| 144 |
/** |
|---|
| 145 |
* 月を示す文字列を返します. <br /> |
|---|
| 146 |
* type の指定によって得られる値<br /> |
|---|
| 147 |
* 0 : JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER<br /> |
|---|
| 148 |
* 1 : January, February, March, April, May, June, July, August, September, October, November, December<br /> |
|---|
| 149 |
* 2 : january, february, march, april, may, june, july, august, september, october, november, december<br /> |
|---|
| 150 |
* 3 : JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC<br /> |
|---|
| 151 |
* 4 : Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec<br /> |
|---|
| 152 |
* 5 : jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec<br /> |
|---|
| 153 |
* 6 : 1月, 2月, 3月, 4月, 5月, 6月, 7月, 8月, 9月, 10月, 11月, 12月<br /> |
|---|
| 154 |
* 7 : 睦月, 如月, 弥生, 卯月, 皐月, 水無月, 文月, 葉月, 長月, 神無月, 霜月, 師走<br /> |
|---|
| 155 |
* |
|---|
| 156 |
* @param index 月 |
|---|
| 157 |
* @param type 取得する月名のタイプ(0から7まで) |
|---|
| 158 |
* @return 月名 |
|---|
| 159 |
* @author michi at seyself.com |
|---|
| 160 |
*/ |
|---|
| 161 |
public static function getMonthName( index:uint , type:uint=0 ):String |
|---|
| 162 |
{ |
|---|
| 163 |
var month:Array; |
|---|
| 164 |
if(type==0) month = ["JANUARY","FEBRUARY","MARCH","APRIL","MAY","JUNE","JULY","AUGUST","SEPTEMBER","OCTOBER","NOVEMBER","DECEMBER"]; |
|---|
| 165 |
else if(type==1) month = ["January","February","March","April","May","June","July","August","September","October","November","December"]; |
|---|
| 166 |
else if(type==2) month = ["january","february","march","april","may","june","july","august","september","october","november","december"]; |
|---|
| 167 |
else if(type==3) month = ["JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"]; |
|---|
| 168 |
else if(type==4) month = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]; |
|---|
| 169 |
else if(type==5) month = ["jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"]; |
|---|
| 170 |
else if(type==6) month = ["1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"]; |
|---|
| 171 |
else if(type==7) month = ["睦月","如月","弥生","卯月","皐月","水無月","文月","葉月","長月","神無月","霜月","師走"]; |
|---|
| 172 |
else month = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]; |
|---|
| 173 |
return month[index]; |
|---|
| 174 |
} |
|---|
| 175 |
|
|---|
| 176 |
/** |
|---|
| 177 |
* 引数 date から相対的な日を表す文字列を返します。<br /> |
|---|
| 178 |
* type の値は getWeekName のものと同じです<br /> |
|---|
| 179 |
* |
|---|
| 180 |
* @param date Dateオブジェクト |
|---|
| 181 |
* @param type 取得する曜日名のタイプ(0から8まで) |
|---|
| 182 |
* @return 曜日名/今日/明日 |
|---|
| 183 |
* @author nagase at ngsdev.org |
|---|
| 184 |
*/ |
|---|
| 185 |
public static function getRelativeDateName( date:Date, type:uint=0 ):String |
|---|
| 186 |
{ |
|---|
| 187 |
var d1:Date = new Date(); |
|---|
| 188 |
d1 = new Date(d1.getFullYear(),d1.getMonth(),d1.getDate()); |
|---|
| 189 |
var d2:Date = new Date(date.getFullYear(),date.getMonth(),date.getDate()); |
|---|
| 190 |
if(d1<d2||d1.getTime()-d2.getTime()>1000*60*60*24*7) return ""; |
|---|
| 191 |
if(type>8||isNaN(type)) type = 0; |
|---|
| 192 |
var list:Array; |
|---|
| 193 |
switch(type) { |
|---|
| 194 |
case 0: case 3: |
|---|
| 195 |
list = ["TODAY","YESTERDAY"]; break; |
|---|
| 196 |
case 1: case 4: |
|---|
| 197 |
list = ["Today","Yesterday"]; break; |
|---|
| 198 |
case 2: case 5: |
|---|
| 199 |
list = ["today","yesterday"]; break; |
|---|
| 200 |
case 6: case 7: case 8: |
|---|
| 201 |
list = ["今日","昨日"]; break; |
|---|
| 202 |
} |
|---|
| 203 |
var diff:int = d1.getDate()-d2.getDate(); |
|---|
| 204 |
if(diff<=1) return list[diff]; |
|---|
| 205 |
return getWeekName( d2.getDay(), type ); |
|---|
| 206 |
} |
|---|
| 207 |
|
|---|
| 208 |
|
|---|
| 209 |
} |
|---|
| 210 |
} |
|---|
| 211 |
|
|---|
| 212 |
|
|---|
| 213 |
|
|---|
| 214 |
|
|---|
| 215 |
|
|---|