格式化日期 String
SimpleDateFormat sdf = new SimpleDateFormat("yyyy
Date date = sdf
String sdate = sdf
package com
import java
import java
import java
import java
import java
import java
public class TimeTest {
//用來全局控制 上一周
private int weeks =
private int MaxDate;//一月最大天數
private int MaxYear;//一年最大天數
/**
* @param args
*/
public static void main(String[] args) {
TimeTest tt = new TimeTest();
System
System
System
System
System
System
System
System
System
System
System
System
System
System
System
System
System
System
System
System
System
System
System
}
/**
* 得到指定月後(前)的日期 參數傳負數即可
*/
public static String getAfterMonth(int month) {
Calendar c = Calendar
SimpleDateFormat sdf = new SimpleDateFormat("yyyy
Date date = null;
try{
date = sdf
}catch(Exception e){
}
c
c
String strDate = sdf
return strDate;
}
/**
* 得到二個日期間的間隔天數
*/
public static String getTwoDay(String sj
SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy
long day =
try {
java
java
day = (date
} catch (Exception e) {
return "";
}
return day + "";
}
/**
* 根據一個日期
*/
public static String getWeek(String sdate) {
// 再轉換為時間
Date date = TimeTest
Calendar c = Calendar
c
// int hour=c
// hour 中存的就是星期幾了
//
return new SimpleDateFormat("EEEE")
}
/**
* 將短時間格式字符串轉換為時間 yyyy
*/
public static Date strToDate(String strDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy
ParsePosition pos = new ParsePosition(
Date strtodate = formatter
return strtodate;
}
/**
* 兩個時間之間的天數
*
* @param date
* @param date
* @return
*/
public static long getDays(String date
if (date
return
if (date
return
// 轉換為標准時間
SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy
java
java
try {
date = myFormatter
mydate = myFormatter
} catch (Exception e) {
}
long day = (date
return day;
}
// 計算當月最後一天
public String getDefaultDay(){
String str = "";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy
Calendar lastDate = Calendar
lastDate
lastDate
lastDate
str=sdf
return str;
}
// 上月第一天
public String getPreviousMonthFirst(){
String str = "";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy
Calendar lastDate = Calendar
lastDate
lastDate
//lastDate
str=sdf
return str;
}
//獲取當月第一天
public String getFirstDayOfMonth(){
String str = "";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy
Calendar lastDate = Calendar
lastDate
str=sdf
return str;
}
// 獲得本周星期日的日期
public String getCurrentWeekday() {
weeks =
int mondayPlus = this
GregorianCalendar currentDate = new GregorianCalendar();
currentDate
Date monday = currentDate
DateFormat df = DateFormat
String preMonday = df
return preMonday;
}
//獲取當天時間
public String getNowTime(String dateformat){
Date now = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat(dateformat);//可
以方便
地修改日期格式
String hehe = dateFormat
return hehe;
}
// 獲得當前日期與本周日相差的天數
private int getMondayPlus() {
Calendar cd = Calendar
// 獲得今天是一周的第幾天
int dayOfWeek = cd
作為第一
天所以這裡減
if (dayOfWeek ==
return
} else {
return
}
}
//獲得本周一的日期
public String getMondayOFWeek(){
weeks =
int mondayPlus = this
GregorianCalendar currentDate = new GregorianCalendar();
currentDate
Date monday = currentDate
DateFormat df = DateFormat
String preMonday = df
return preMonday;
}
//獲得相應周的周六的日期
public String getSaturday() {
int mondayPlus = this
GregorianCalendar currentDate = new GregorianCalendar();
currentDate
Date monday = currentDate
DateFormat df = DateFormat
String preMonday = df
return preMonday;
}
// 獲得上周星期日的日期
public String getPreviousWeekSunday() {
weeks=
weeks
int mondayPlus = this
GregorianCalendar currentDate = new GregorianCalendar();
currentDate
Date monday = currentDate
DateFormat df = DateFormat
String preMonday = df
return preMonday;
}
// 獲得上周星期一的日期
public String getPreviousWeekday() {
weeks
int mondayPlus = this
GregorianCalendar currentDate = new GregorianCalendar();
currentDate
Date monday = currentDate
DateFormat df = DateFormat
String preMonday = df
return preMonday;
}
// 獲得下周星期一的日期
public String getNextMonday() {
weeks++;
int mondayPlus = this
GregorianCalendar currentDate = new GregorianCalendar();
currentDate
Date monday = currentDate
DateFormat df = DateFormat
String preMonday = df
return preMonday;
}
// 獲得下周星期日的日期
public String getNextSunday() {
int mondayPlus = this
GregorianCalendar currentDate = new GregorianCalendar();
currentDate
Date monday = currentDate
DateFormat df = DateFormat
String preMonday = df
return preMonday;
}
private int getMonthPlus(){
Calendar cd = Calendar
int monthOfNumber = cd
cd
cd
MaxDate=cd
if(monthOfNumber ==
return
}else{
return
}
}
//獲得上月最後一天的日期
public String getPreviousMonthEnd(){
String str = "";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy
Calendar lastDate = Calendar
lastDate
lastDate
lastDate
str=sdf
return str;
}
//獲得下個月第一天的日期
public String getNextMonthFirst(){
String str = "";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy
Calendar lastDate = Calendar
lastDate
lastDate
str=sdf
return str;
}
//獲得下個月最後一天的日期
public String getNextMonthEnd(){
String str = "";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy
Calendar lastDate = Calendar
lastDate
lastDate
lastDate
str=sdf
return str;
}
//獲得明年最後一天的日期
public String getNextYearEnd(){
String str = "";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy
Calendar lastDate = Calendar
lastDate
lastDate
lastDate
str=sdf
return str;
}
//獲得明年第一天的日期
public String getNextYearFirst(){
String str = "";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy
Calendar lastDate = Calendar
lastDate
lastDate
str=sdf
return str;
}
//獲得本年有多少天
private int getMaxYear(){
Calendar cd = Calendar
cd
cd
int MaxYear = cd
return MaxYear;
}
private int getYearPlus(){
Calendar cd = Calendar
int yearOfNumber = cd
cd
cd
int MaxYear = cd
if(yearOfNumber ==
return
}else{
return
}
}
//獲得本年第一天的日期
public String getCurrentYearFirst(){
int yearPlus = this
GregorianCalendar currentDate = new GregorianCalendar();
currentDate
Date yearDay = currentDate
DateFormat df = DateFormat
String preYearDay = df
return preYearDay;
}
//獲得本年最後一天的日期 *
public String getCurrentYearEnd(){
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");//可以方
便地修
改日期格式
String years = dateFormat
return years+"
}
//獲得上年第一天的日期 *
public String getPreviousYearFirst(){
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");//可以方
便地修
改日期格式
String years = dateFormat
(years);
years_value
return years_value+"
}
//獲得上年最後一天的日期
public String getPreviousYearEnd(){
weeks
int yearPlus = this
GregorianCalendar currentDate = new GregorianCalendar();
currentDate
Date yearDay = currentDate
DateFormat df = DateFormat
String preYearDay = df
getThisSeasonTime(
return preYearDay;
}
//獲得本季度
public String getThisSeasonTime(int month){
int array[][] = {{
int season =
if(month>=
season =
}
if(month>=
season =
}
if(month>=
season =
}
if(month>=
season =
}
int start_month = array[season
int end_month = array[season
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");//可以方
便地修
改日期格式
String years = dateFormat
int years_value = Integer
int start_days =
";//getLastDayOfMonth(years_value
int end_days = getLastDayOfMonth(years_value
String seasonDate =
years_value+"
return seasonDate;
}
/**
* 獲取某年某月的最後一天
* @param year 年
* @param month 月
* @return 最後一天
*/
private int getLastDayOfMonth(int year
if (month ==
|| month ==
return
}
if (month ==
return
}
if (month ==
if (isLeapYear(year)) {
return
} else {
return
}
}
return
}
/**
* 是否閏年
* @param year 年
* @return
*/
public boolean isLeapYear(int year) {
return (year %
}
From:http://tw.wingwit.com/Article/program/Java/JSP/201311/20190.html