日期 日歷 時區 地區【常用案例】


2017-6-9

判斷是今天還是明天

public static String getDayString(long millseconds) {
	Calendar calendar = Calendar.getInstance();
	calendar.set(Calendar.HOUR_OF_DAY, 23);
	calendar.set(Calendar.MINUTE, 59);
	calendar.set(Calendar.SECOND, 59);

	if (millseconds > calendar.getTimeInMillis()) return "明天";
	else return "今天";
}

計算兩個日期間相差幾天

//計算兩個日期間相差幾天
public static long getDay(String dateStr1, String sFormat1, String dateStr2, String sFormat2) {
	try {
		Date date1 = new SimpleDateFormat(sFormat1).parse(dateStr1);
		Date date2 = new SimpleDateFormat(sFormat2).parse(dateStr2);
		Long time = Math.abs(date1.getTime() - date2.getTime());
		return time / 1000 / 60 / 60 / 24;
	} catch (ParseException e) {
		e.printStackTrace();
	}
	return -1;
}

增加或減少一定的時間

//增加或減少一定的時間
public static Date addDate(Date date, int field, int amount) {
	Calendar calendar = Calendar.getInstance();
	calendar.setTime(date);
	calendar.add(field, amount); //月 Calendar.MONTH;天 Calendar.DAY_OF_YEAR;小時 Calendar.HOUR;分 Calendar.MINUTE;秒 Calendar.SECOND
	return calendar.getTime();
}

判斷緩存是否過期

System.currentTimeMillis() / 1000 / 60 / 60 / 24 / 2 + "_bqt";//緩存2天

獲取一個時間,要求當月有31天

public static long getAGoodTime() {
	Calendar calendar = Calendar.getInstance();
	calendar.set(Calendar.YEAR, 2017);
	calendar.set(Calendar.MONTH, 5);
	calendar.set(Calendar.DAY_OF_MONTH, 16);

	return calendar.getTimeInMillis();
}

SimpleDateFormat格式化Date示例

默認的格式
System.out.println(SimpleDateFormat.getInstance().format(new Date()));//16-5-1 下午7:47
System.out.println(SimpleDateFormat.getDateInstance().format(new Date()));//2016-5-1
System.out.println(SimpleDateFormat.getTimeInstance().format(new Date()));//19:46:04
System.out.println(SimpleDateFormat.getDateTimeInstance().format(new Date()));//2016-5-1 19:45:10
不同風格格式 Date
System.out.println(SimpleDateFormat.getDateInstance(DateFormat.DEFAULT).format(date));//2016-9-1
System.out.println(SimpleDateFormat.getDateInstance(DateFormat.SHORT).format(date));//16-9-1
System.out.println(SimpleDateFormat.getDateInstance(DateFormat.MEDIUM).format(date));//2016-9-1
System.out.println(SimpleDateFormat.getDateInstance(DateFormat.LONG).format(date));//2016年9月1日
System.out.println(SimpleDateFormat.getDateInstance(DateFormat.FULL).format(date) + "\n");//2016年9月1日 星期四
不同風格格式 Time
System.out.println(SimpleDateFormat.getTimeInstance(DateFormat.DEFAULT).format(date));//18:04:37
System.out.println(SimpleDateFormat.getTimeInstance(DateFormat.SHORT).format(date));//下午6:04
System.out.println(SimpleDateFormat.getTimeInstance(DateFormat.MEDIUM).format(date));//18:04:37
System.out.println(SimpleDateFormat.getTimeInstance(DateFormat.LONG).format(date));//下午06時04分37秒
System.out.println(SimpleDateFormat.getTimeInstance(DateFormat.FULL).format(date));//下午06時04分37秒 CST
自定義格式
System.out.println(new SimpleDateFormat("yyyy.MM.dd a HH:mm:ss SSS", Locale.getDefault()).format(new Date()));//2016.05.01 下午 22:29:46 583
System.out.println(new SimpleDateFormat("M月d日 m分 第D天 H點(ah點) s秒S微秒").format(new Date()));//5月1日 57分 第122天 22點(下午10點)  33秒95微秒
System.out.println(new SimpleDateFormat("z(zzzz) Gyy年第w周 M月第W周 E").format(new Date()));//CST(中國標准時間) 公元16年第19周 5月第1周 星期日

Calendar和Date的妙用

public static void main(String[] args) throws ParseException {
	SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	Date date = format.parse("2016-05-1 19:45:8");
	System.out.println(format.format(date));//2016-05-01 19:45:08
	System.out.println(format.format(reFormatDate(date)));//2017-10-16 19:45:08
}

public static Date reFormatDate(Date date) {
	Calendar calendar = Calendar.getInstance();
	calendar.setTime(date);// date中僅時和分時有效的,年月日重新格式化
	calendar.set(Calendar.YEAR, Calendar.getInstance().get(Calendar.YEAR));
	calendar.set(Calendar.MONTH, Calendar.getInstance().get(Calendar.MONTH));
	calendar.set(Calendar.DAY_OF_MONTH, Calendar.getInstance().get(Calendar.DAY_OF_MONTH));
	return calendar.getTime();
}

Calendar基本操作示例

Calendar calendar = Calendar.getInstance();
//年月日
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;//這里一定要注意:0代表1月,11表示十二月,12表示下一年的1月…
int day = calendar.get(Calendar.DAY_OF_MONTH);
//上下午
String apStr = calendar.get(Calendar.AM_PM) == 1 ? "下午" : "上午";//0代表上午,1代表下午
//時分秒
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);//由 0 至 61 的整數表示,值 60 和 61 只對閏秒發生
//星期
String[] weeks = { "日", "一", "二", "三", "四", "五", "六" };
int weekNum = calendar.get(Calendar.DAY_OF_WEEK);//1表示星期日,7表示星期六
String weekStr = "星期" + weeks[weekNum - 1];

String string = year + "年" + month + "月" + day + "日" + " " + weekStr + " " + apStr + hour + ":" + minute + ":" + second;
System.out.println(string);//2016年5月1日 星期日 下午23:27:26

TimeZone中可獲取到的有用信息

TimeZone timeZone = TimeZone.getDefault();//獲取此主機的默認 TimeZone
System.out.println(timeZone.getDisplayName());//中國標准時間
System.out.println(timeZone.getDisplayName(true, TimeZone.SHORT));//CDT。如果為 true,則返回夏令時名稱
System.out.println(timeZone.getDisplayName(false, TimeZone.SHORT));//CST
System.out.println(timeZone.getDisplayName(true, TimeZone.LONG));//中國夏令時
System.out.println(timeZone.getDisplayName(false, TimeZone.LONG));//中國標准時間

System.out.println(timeZone.getID());//Asia/Shanghai
System.out.println(timeZone.getRawOffset() / 1000 / 60 / 60);//8。也就是8小時,因為我們是在東八區
System.out.println(Arrays.toString(TimeZone.getAvailableIDs()));//獲取受支持的所有可用ID
所有可用ID
【所有可用ID】
[Africa/Abidjan, Africa/Accra, Africa/Addis_Ababa, Africa/Algiers, Africa/Asmara, Africa/Asmera, Africa/Bamako, Africa/Bangui, Africa/Banjul, Africa/Bissau, Africa/Blantyre, Africa/Brazzaville, Africa/Bujumbura, Africa/Cairo, Africa/Casablanca, Africa/Ceuta, Africa/Conakry, Africa/Dakar, Africa/Dar_es_Salaam, Africa/Djibouti, Africa/Douala, Africa/El_Aaiun, Africa/Freetown, Africa/Gaborone, Africa/Harare, Africa/Johannesburg, Africa/Juba, Africa/Kampala, Africa/Khartoum, Africa/Kigali, Africa/Kinshasa, Africa/Lagos, Africa/Libreville, Africa/Lome, Africa/Luanda, Africa/Lubumbashi, Africa/Lusaka, Africa/Malabo, Africa/Maputo, Africa/Maseru, Africa/Mbabane, Africa/Mogadishu, Africa/Monrovia, Africa/Nairobi, Africa/Ndjamena, Africa/Niamey, Africa/Nouakchott, Africa/Ouagadougou, Africa/Porto-Novo, Africa/Sao_Tome, Africa/Timbuktu, Africa/Tripoli, Africa/Tunis, Africa/Windhoek, America/Adak, America/Anchorage, America/Anguilla, America/Antigua, America/Araguaina, America/Argentina/Buenos_Aires, America/Argentina/Catamarca, America/Argentina/ComodRivadavia, America/Argentina/Cordoba, America/Argentina/Jujuy, America/Argentina/La_Rioja, America/Argentina/Mendoza, America/Argentina/Rio_Gallegos, America/Argentina/Salta, America/Argentina/San_Juan, America/Argentina/San_Luis, America/Argentina/Tucuman, America/Argentina/Ushuaia, America/Aruba, America/Asuncion, America/Atikokan, America/Atka, America/Bahia, America/Bahia_Banderas, America/Barbados, America/Belem, America/Belize, America/Blanc-Sablon, America/Boa_Vista, America/Bogota, America/Boise, America/Buenos_Aires, America/Cambridge_Bay, America/Campo_Grande, America/Cancun, America/Caracas, America/Catamarca, America/Cayenne, America/Cayman, America/Chicago, America/Chihuahua, America/Coral_Harbour, America/Cordoba, America/Costa_Rica, America/Creston, America/Cuiaba, America/Curacao, America/Danmarkshavn, America/Dawson, America/Dawson_Creek, America/Denver, America/Detroit, America/Dominica, America/Edmonton, America/Eirunepe, America/El_Salvador, America/Ensenada, America/Fort_Nelson, America/Fort_Wayne, America/Fortaleza, America/Glace_Bay, America/Godthab, America/Goose_Bay, America/Grand_Turk, America/Grenada, America/Guadeloupe, America/Guatemala, America/Guayaquil, America/Guyana, America/Halifax, America/Havana, America/Hermosillo, America/Indiana/Indianapolis, America/Indiana/Knox, America/Indiana/Marengo, America/Indiana/Petersburg, America/Indiana/Tell_City, America/Indiana/Vevay, America/Indiana/Vincennes, America/Indiana/Winamac, America/Indianapolis, America/Inuvik, America/Iqaluit, America/Jamaica, America/Jujuy, America/Juneau, America/Kentucky/Louisville, America/Kentucky/Monticello, America/Knox_IN, America/Kralendijk, America/La_Paz, America/Lima, America/Los_Angeles, America/Louisville, America/Lower_Princes, America/Maceio, America/Managua, America/Manaus, America/Marigot, America/Martinique, America/Matamoros, America/Mazatlan, America/Mendoza, America/Menominee, America/Merida, America/Metlakatla, America/Mexico_City, America/Miquelon, America/Moncton, America/Monterrey, America/Montevideo, America/Montreal, America/Montserrat, America/Nassau, America/New_York, America/Nipigon, America/Nome, America/Noronha, America/North_Dakota/Beulah, America/North_Dakota/Center, America/North_Dakota/New_Salem, America/Ojinaga, America/Panama, America/Pangnirtung, America/Paramaribo, America/Phoenix, America/Port-au-Prince, America/Port_of_Spain, America/Porto_Acre, America/Porto_Velho, America/Puerto_Rico, America/Rainy_River, America/Rankin_Inlet, America/Recife, America/Regina, America/Resolute, America/Rio_Branco, America/Rosario, America/Santa_Isabel, America/Santarem, America/Santiago, America/Santo_Domingo, America/Sao_Paulo, America/Scoresbysund, America/Shiprock, America/Sitka, America/St_Barthelemy, America/St_Johns, America/St_Kitts, America/St_Lucia, America/St_Thomas, America/St_Vincent, America/Swift_Current, America/Tegucigalpa, America/Thule, America/Thunder_Bay, America/Tijuana, America/Toronto, America/Tortola, America/Vancouver, America/Virgin, America/Whitehorse, America/Winnipeg, America/Yakutat, America/Yellowknife, Antarctica/Casey, Antarctica/Davis, Antarctica/DumontDUrville, Antarctica/Macquarie, Antarctica/Mawson, Antarctica/McMurdo, Antarctica/Palmer, Antarctica/Rothera, Antarctica/South_Pole, Antarctica/Syowa, Antarctica/Troll, Antarctica/Vostok, Arctic/Longyearbyen, Asia/Aden, Asia/Almaty, Asia/Amman, Asia/Anadyr, Asia/Aqtau, Asia/Aqtobe, Asia/Ashgabat, Asia/Ashkhabad, Asia/Baghdad, Asia/Bahrain, Asia/Baku, Asia/Bangkok, Asia/Barnaul, Asia/Beirut, Asia/Bishkek, Asia/Brunei, Asia/Calcutta, Asia/Chita, Asia/Choibalsan, Asia/Chongqing, Asia/Chungking, Asia/Colombo, Asia/Dacca, Asia/Damascus, Asia/Dhaka, Asia/Dili, Asia/Dubai, Asia/Dushanbe, Asia/Famagusta, Asia/Gaza, Asia/Harbin, Asia/Hebron, Asia/Ho_Chi_Minh, Asia/Hong_Kong, Asia/Hovd, Asia/Irkutsk, Asia/Istanbul, Asia/Jakarta, Asia/Jayapura, Asia/Jerusalem, Asia/Kabul, Asia/Kamchatka, Asia/Karachi, Asia/Kashgar, Asia/Kathmandu, Asia/Katmandu, Asia/Khandyga, Asia/Kolkata, Asia/Krasnoyarsk, Asia/Kuala_Lumpur, Asia/Kuching, Asia/Kuwait, Asia/Macao, Asia/Macau, Asia/Magadan, Asia/Makassar, Asia/Manila, Asia/Muscat, Asia/Nicosia, Asia/Novokuznetsk, Asia/Novosibirsk, Asia/Omsk, Asia/Oral, Asia/Phnom_Penh, Asia/Pontianak, Asia/Pyongyang, Asia/Qatar, Asia/Qyzylorda, Asia/Rangoon, Asia/Riyadh, Asia/Saigon, Asia/Sakhalin, Asia/Samarkand, Asia/Seoul, Asia/Shanghai, Asia/Singapore, Asia/Srednekolymsk, Asia/Taipei, Asia/Tashkent, Asia/Tbilisi, Asia/Tehran, Asia/Tel_Aviv, Asia/Thimbu, Asia/Thimphu, Asia/Tokyo, Asia/Tomsk, Asia/Ujung_Pandang, Asia/Ulaanbaatar, Asia/Ulan_Bator, Asia/Urumqi, Asia/Ust-Nera, Asia/Vientiane, Asia/Vladivostok, Asia/Yakutsk, Asia/Yangon, Asia/Yekaterinburg, Asia/Yerevan, Atlantic/Azores, Atlantic/Bermuda, Atlantic/Canary, Atlantic/Cape_Verde, Atlantic/Faeroe, Atlantic/Faroe, Atlantic/Jan_Mayen, Atlantic/Madeira, Atlantic/Reykjavik, Atlantic/South_Georgia, Atlantic/St_Helena, Atlantic/Stanley, Australia/ACT, Australia/Adelaide, Australia/Brisbane, Australia/Broken_Hill, Australia/Canberra, Australia/Currie, Australia/Darwin, Australia/Eucla, Australia/Hobart, Australia/LHI, Australia/Lindeman, Australia/Lord_Howe, Australia/Melbourne, Australia/NSW, Australia/North, Australia/Perth, Australia/Queensland, Australia/South, Australia/Sydney, Australia/Tasmania, Australia/Victoria, Australia/West, Australia/Yancowinna, Brazil/Acre, Brazil/DeNoronha, Brazil/East, Brazil/West, CET, CST6CDT, Canada/Atlantic, Canada/Central, Canada/East-Saskatchewan, Canada/Eastern, Canada/Mountain, Canada/Newfoundland, Canada/Pacific, Canada/Saskatchewan, Canada/Yukon, Chile/Continental, Chile/EasterIsland, Cuba, EET, EST5EDT, Egypt, Eire, Etc/GMT, Etc/GMT+0, Etc/GMT+1, Etc/GMT+10, Etc/GMT+11, Etc/GMT+12, Etc/GMT+2, Etc/GMT+3, Etc/GMT+4, Etc/GMT+5, Etc/GMT+6, Etc/GMT+7, Etc/GMT+8, Etc/GMT+9, Etc/GMT-0, Etc/GMT-1, Etc/GMT-10, Etc/GMT-11, Etc/GMT-12, Etc/GMT-13, Etc/GMT-14, Etc/GMT-2, Etc/GMT-3, Etc/GMT-4, Etc/GMT-5, Etc/GMT-6, Etc/GMT-7, Etc/GMT-8, Etc/GMT-9, Etc/GMT0, Etc/Greenwich, Etc/UCT, Etc/UTC, Etc/Universal, Etc/Zulu, Europe/Amsterdam, Europe/Andorra, Europe/Astrakhan, Europe/Athens, Europe/Belfast, Europe/Belgrade, Europe/Berlin, Europe/Bratislava, Europe/Brussels, Europe/Bucharest, Europe/Budapest, Europe/Busingen, Europe/Chisinau, Europe/Copenhagen, Europe/Dublin, Europe/Gibraltar, Europe/Guernsey, Europe/Helsinki, Europe/Isle_of_Man, Europe/Istanbul, Europe/Jersey, Europe/Kaliningrad, Europe/Kiev, Europe/Kirov, Europe/Lisbon, Europe/Ljubljana, Europe/London, Europe/Luxembourg, Europe/Madrid, Europe/Malta, Europe/Mariehamn, Europe/Minsk, Europe/Monaco, Europe/Moscow, Europe/Nicosia, Europe/Oslo, Europe/Paris, Europe/Podgorica, Europe/Prague, Europe/Riga, Europe/Rome, Europe/Samara, Europe/San_Marino, Europe/Sarajevo, Europe/Simferopol, Europe/Skopje, Europe/Sofia, Europe/Stockholm, Europe/Tallinn, Europe/Tirane, Europe/Tiraspol, Europe/Ulyanovsk, Europe/Uzhgorod, Europe/Vaduz, Europe/Vatican, Europe/Vienna, Europe/Vilnius, Europe/Volgograd, Europe/Warsaw, Europe/Zagreb, Europe/Zaporozhye, Europe/Zurich, GB, GB-Eire, GMT, GMT0, Greenwich, Hongkong, Iceland, Indian/Antananarivo, Indian/Chagos, Indian/Christmas, Indian/Cocos, Indian/Comoro, Indian/Kerguelen, Indian/Mahe, Indian/Maldives, Indian/Mauritius, Indian/Mayotte, Indian/Reunion, Iran, Israel, Jamaica, Japan, Kwajalein, Libya, MET, MST7MDT, Mexico/BajaNorte, Mexico/BajaSur, Mexico/General, NZ, NZ-CHAT, Navajo, PRC, PST8PDT, Pacific/Apia, Pacific/Auckland, Pacific/Bougainville, Pacific/Chatham, Pacific/Chuuk, Pacific/Easter, Pacific/Efate, Pacific/Enderbury, Pacific/Fakaofo, Pacific/Fiji, Pacific/Funafuti, Pacific/Galapagos, Pacific/Gambier, Pacific/Guadalcanal, Pacific/Guam, Pacific/Honolulu, Pacific/Johnston, Pacific/Kiritimati, Pacific/Kosrae, Pacific/Kwajalein, Pacific/Majuro, Pacific/Marquesas, Pacific/Midway, Pacific/Nauru, Pacific/Niue, Pacific/Norfolk, Pacific/Noumea, Pacific/Pago_Pago, Pacific/Palau, Pacific/Pitcairn, Pacific/Pohnpei, Pacific/Ponape, Pacific/Port_Moresby, Pacific/Rarotonga, Pacific/Saipan, Pacific/Samoa, Pacific/Tahiti, Pacific/Tarawa, Pacific/Tongatapu, Pacific/Truk, Pacific/Wake, Pacific/Wallis, Pacific/Yap, Poland, Portugal, ROK, Singapore, SystemV/AST4, SystemV/AST4ADT, SystemV/CST6, SystemV/CST6CDT, SystemV/EST5, SystemV/EST5EDT, SystemV/HST10, SystemV/MST7, SystemV/MST7MDT, SystemV/PST8, SystemV/PST8PDT, SystemV/YST9, SystemV/YST9YDT, Turkey, UCT, US/Alaska, US/Aleutian, US/Arizona, US/Central, US/East-Indiana, US/Eastern, US/Hawaii, US/Indiana-Starke, US/Michigan, US/Mountain, US/Pacific, US/Pacific-New, US/Samoa, UTC, Universal, W-SU, WET, Zulu, EST, HST, MST, ACT, AET, AGT, ART, AST, BET, BST, CAT, CNT, CST, CTT, EAT, ECT, IET, IST, JST, MIT, NET, NST, PLT, PNT, PRT, PST, SST, VST]

Locale中可獲取到的有用信息

Locale locale = Locale.getDefault();//獲得此 Java 虛擬機實例的當前默認語言環境值
System.out.println(locale.getCountry());//CN。國家
System.out.println(locale.getDisplayCountry());//中國
System.out.println(locale.getLanguage());//zh。語言
System.out.println(locale.getDisplayLanguage());//中文

System.out.println(Locale.CHINESE);//zh。代表中文環境
System.out.println(Locale.CHINA);//zh_CN。代表中國大陸的中文環境
System.out.println(Locale.TAIWAN);//zh_TW。代表中國台灣的中文環境
System.out.println(Locale.ENGLISH);//en。代表英文環境
System.out.println(Locale.US);//en_US。代表美國的英文環境
System.out.println(Locale.UK);//en_GB。代表英國的英文環境

System.out.println(Arrays.toString(Locale.getAvailableLocales()));//返回所有已安裝語言環境的數組
所有已安裝語言環境
【所有已安裝語言環境】
[, ar_AE, ar_JO, ar_SY, hr_HR, fr_BE, es_PA, mt_MT, es_VE, bg, zh_TW, it, ko, uk, lv, da_DK, es_PR, vi_VN, en_US, sr_ME, sv_SE, es_BO, en_SG, ar_BH, pt, ar_SA, sk, ar_YE, hi_IN, ga, en_MT, fi_FI, et, sv, cs, sr_BA_#Latn, el, uk_UA, hu, fr_CH, in, es_AR, ar_EG, ja_JP_JP_#u-ca-japanese, es_SV, pt_BR, be, is_IS, cs_CZ, es, pl_PL, tr, ca_ES, sr_CS, ms_MY, hr, lt, es_ES, es_CO, bg_BG, sq, fr, ja, sr_BA, is, es_PY, de, es_EC, es_US, ar_SD, en, ro_RO, en_PH, ca, ar_TN, sr_ME_#Latn, es_GT, sl, ko_KR, el_CY, es_MX, ru_RU, es_HN, zh_HK, no_NO_NY, hu_HU, th_TH, ar_IQ, es_CL, fi, ar_MA, ga_IE, mk, tr_TR, et_EE, ar_QA, sr__#Latn, pt_PT, fr_LU, ar_OM, th, sq_AL, es_DO, es_CU, ar, ru, en_NZ, sr_RS, de_CH, es_UY, ms, el_GR, iw_IL, en_ZA, th_TH_TH_#u-nu-thai, hi, fr_FR, de_AT, nl, no_NO, en_AU, vi, nl_NL, fr_CA, lv_LV, de_LU, es_CR, ar_KW, sr, ar_LY, mt, it_CH, da, de_DE, ar_DZ, sk_SK, lt_LT, it_IT, en_IE, zh_SG, ro, en_CA, nl_BE, no, pl, zh_CN, ja_JP, de_GR, sr_RS_#Latn, iw, en_IN, ar_LB, es_NI, zh, mk_MK, be_BY, sl_SI, es_PE, in_ID, en_GB]
2017-8-25




关注微信公众号

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2020 ITdaan.com