一个Calendar的例子
package org.oa;
import java.util.*;
import java.text.*;
public class nowDate {
public Date GetNowDate() {
SimpleDateFormat formatter =
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
java.util.Date currentTime_1 = new java.util.Date();
String dateString = formatter.format(currentTime_1);
ParsePosition pos = new ParsePosition(8);
Date currentTime_2 = formatter.parse(dateString, pos);
return currentTime_2;
}
public String GetStringDate() {
SimpleDateFormat formatter =
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
java.util.Date currentTime_1 = new java.util.Date();
String dateString = formatter.format(currentTime_1);
return dateString;
}
public String getDayDate() {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date currentTime_1 = new java.util.Date();
String dateString = formatter.format(currentTime_1);
return dateString;
}
public String getNowWeek() {
String Targetstr = GetStringDate();
String tempStrI[] = new String[3];
tempStrI[0] = Targetstr.substring(0, 4);
tempStrI[1] = Targetstr.substring(5, 7);
tempStrI[2] = Targetstr.substring(8, 10);
Calendar cal = Calendar.getInstance();
cal.set(
Integer.parseInt(tempStrI[0]) - 1900,
Integer.parseInt(tempStrI[1]),
Integer.parseInt(tempStrI[2]));
int NowWeek = cal.get(Calendar.WEEK_OF_MONTH);
cal.add(Calendar.DAY_OF_YEAR, -NowWeek);
int stryear = cal.get(Calendar.YEAR);
int strmonth = cal.get(Calendar.MONTH);
int strday = cal.get(Calendar.DAY_OF_MONTH);
return new String(
"" + (stryear + 1900) + "-" + strmonth + "-" + strday);
}
public String getLastNumWeek(int LastNum) {
String Targetstr = GetStringDate();
String tempStrI[] = new String[3];
tempStrI[0] = Targetstr.substring(0, 4);
tempStrI[1] = Targetstr.substring(5, 7);
tempStrI[2] = Targetstr.substring(8, 10);
Calendar cal = Calendar.getInstance();
cal.set(
Integer.parseInt(tempStrI[0]) - 1900,
Integer.parseInt(tempStrI[1]),
Integer.parseInt(tempStrI[2]));
int NowWeek = cal.get(Calendar.WEEK_OF_MONTH);
cal.add(Calendar.WEEK_OF_MONTH, -LastNum);
cal.add(Calendar.DAY_OF_YEAR, -NowWeek);
int stryear = cal.get(Calendar.YEAR);
int strmonth = cal.get(Calendar.MONTH);
int strday = cal.get(Calendar.DAY_OF_MONTH);
return new String(
"" + (stryear + 1900) + "-" + strmonth + "-" + strday);
}
public String getNowDay() {
String Targetstr = GetStringDate();
String tempStrI[] = new String[3];
tempStrI[0] = Targetstr.substring(0, 4);
tempStrI[1] = Targetstr.substring(5, 7);
tempStrI[2] = Targetstr.substring(8, 10);
return new String(
"" + tempStrI[0] + "-" + tempStrI[1] + "-" + tempStrI[2]);
}
public String getLastNumDay(int LastNum) {
String Targetstr = GetStringDate();
String tempStrI[] = new String[3];
tempStrI[0] = Targetstr.substring(0, 4);
tempStrI[1] = Targetstr.substring(5, 7);
tempStrI[2] = Targetstr.substring(8, 10);
Calendar cal = Calendar.getInstance();
cal.set(
Integer.parseInt(tempStrI[0]) - 1900,
Integer.parseInt(tempStrI[1]),
Integer.parseInt(tempStrI[2]));
cal.add(Calendar.DAY_OF_YEAR, -LastNum);
int stryear = cal.get(Calendar.YEAR);
int strmonth = cal.get(Calendar.MONTH);
int strday = cal.get(Calendar.DAY_OF_MONTH);
return new String(
"" + (stryear + 1900) + "-" + strmonth + "-" + strday);
}
public String getNowMonth() {
String Targetstr = GetStringDate();
String tempStrI[] = new String[3];
tempStrI[0] = Targetstr.substring(0, 4);
tempStrI[1] = Targetstr.substring(5, 7);
tempStrI[2] = Targetstr.substring(8, 10);
Calendar cal = Calendar.getInstance();
cal.set(
Integer.parseInt(tempStrI[0]) - 1900,
Integer.parseInt(tempStrI[1]),
Integer.parseInt(tempStrI[2]));
int stryear = cal.get(Calendar.YEAR);
int strmonth = cal.get(Calendar.MONTH);
return new String("" + (stryear + 1900) + "-" + strmonth + "-" + 1);
}
public String getLastNumMonth(int LastNum) {
String Targetstr = GetStringDate();
String tempStrI[] = new String[3];
tempStrI[0] = Targetstr.substring(0, 4);
tempStrI[1] = Targetstr.substring(5, 7);
tempStrI[2] = Targetstr.substring(8, 10);
Calendar cal = Calendar.getInstance();
cal.set(
Integer.parseInt(tempStrI[0]) - 1900,
Integer.parseInt(tempStrI[1]),
Integer.parseInt(tempStrI[2]));
cal.add(Calendar.MONTH, -1);
int stryear = cal.get(Calendar.YEAR);
int strmonth = cal.get(Calendar.MONTH);
int strday = cal.get(Calendar.DAY_OF_MONTH);
return new String("" + (stryear + 1900) + "-" + strmonth + "-" + 1);
}
public String getNowYear() {
String Targetstr = GetStringDate();
String tempStrI[] = new String[3];
tempStrI[0] = Targetstr.substring(0, 4);
tempStrI[1] = Targetstr.substring(5, 7);
tempStrI[2] = Targetstr.substring(8, 10);
Calendar cal = Calendar.getInstance();
cal.set(
Integer.parseInt(tempStrI[0]) - 1900,
Integer.parseInt(tempStrI[1]),
Integer.parseInt(tempStrI[2]));
int stryear = cal.get(Calendar.YEAR);
return new String("" + (stryear + 1900) + "-" + 1 + "-" + 1);
}
public String getNowYear(int LastNum) {
String Targetstr = GetStringDate();
String tempStrI[] = new String[3];
tempStrI[0] = Targetstr.substring(0, 4);
tempStrI[1] = Targetstr.substring(5, 7);
tempStrI[2] = Targetstr.substring(8, 10);
Calendar cal = Calendar.getInstance();
cal.set(
Integer.parseInt(tempStrI[0]) - 1900,
Integer.parseInt(tempStrI[1]),
Integer.parseInt(tempStrI[2]));
cal.add(Calendar.YEAR, -1);
int stryear = cal.get(Calendar.YEAR);
return new String("" + (stryear + 1900) + "-" + 1 + "-" + 1);
}
public String ChangeDate(
String Targetstr,
String year,
String month,
String day) {
String tempStrI[] = new String[3];
tempStrI[0] = Targetstr.substring(0, 4);
tempStrI[1] = Targetstr.substring(5, 7);
tempStrI[2] = Targetstr.substring(8, 10);
Calendar cal = Calendar.getInstance();
int temp = 0;
cal.set(
Integer.parseInt(tempStrI[0]) - 1900,
Integer.parseInt(tempStrI[1]),
Integer.parseInt(tempStrI[2]));
if ((temp = year.indexOf('-')) > 0)
year = year.substring(temp);
if ((temp = month.indexOf('-')) > 0)
month = month.substring(temp);
if ((temp = day.indexOf('-')) > 0)
day = day.substring(temp);
cal.add(Calendar.YEAR, Integer.parseInt(year));
cal.add(Calendar.MONTH, Integer.parseInt(month));
cal.add(Calendar.DAY_OF_MONTH, Integer.parseInt(day));
int stryear = cal.get(Calendar.YEAR);
int strmonth = cal.get(Calendar.MONTH);
int strday = cal.get(Calendar.DAY_OF_MONTH);
stryear += 1900;
return new String("" + stryear + "-" + strmonth + "-" + strday);
}
public String CompareDate(String SourceDate, String TargetDate) {
String newSource = "";
int tempI = 0;
int tempII = 0;
String newTarget = "";
tempI = SourceDate.indexOf(' ');
tempII = TargetDate.indexOf(' ');
if (tempI > 0 && tempII > 0) {
newSource = SourceDate.substring(0, tempI);
newTarget = TargetDate.substring(0, tempII);
newSource = newSource.trim();
newTarget = newTarget.trim();
} else {
newSource = SourceDate;
newTarget = TargetDate;
}
if (newSource.equals("") || newTarget.equals(""))
return null;
else {
String tempStrI[] = new String[3];
String tempStrII[] = new String[3];
int tempIII = 0;
int tempIIII = 0;
int temp = 0;
tempStrI[0] = newSource.substring(0, 4);
tempStrI[1] = newSource.substring(5, 7);
tempStrI[2] = newSource.substring(8, 10);
tempStrII[0] = newTarget.substring(0, 4);
tempStrII[1] = newTarget.substring(5, 7);
tempStrII[2] = newTarget.substring(8, 10);
tempIII = Integer.parseInt(tempStrI[0]);
tempIIII = Integer.parseInt(tempStrII[0]);
if (tempIII != tempIIII) {
temp = tempIII - tempIII;
temp = temp * 360;
}
tempIII = Integer.parseInt(tempStrI[1]);
tempIIII = Integer.parseInt(tempStrII[1]);
if (tempIII != tempIIII) {
temp = temp + (tempIII - tempIIII) * 30;
}
tempIII = Integer.parseInt(tempStrI[2]);
tempIIII = Integer.parseInt(tempStrII[2]);
if (tempIII != tempIIII) {
temp = temp + (tempIII - tempIIII);
}
if (temp < 0) {
temp = 0 - temp;
return new String("" + temp);
} else
return new String("" + temp);
}
}
}
import java.util.*;
import java.text.*;
public class nowDate {
public Date GetNowDate() {
SimpleDateFormat formatter =
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
java.util.Date currentTime_1 = new java.util.Date();
String dateString = formatter.format(currentTime_1);
ParsePosition pos = new ParsePosition(8);
Date currentTime_2 = formatter.parse(dateString, pos);
return currentTime_2;
}
public String GetStringDate() {
SimpleDateFormat formatter =
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
java.util.Date currentTime_1 = new java.util.Date();
String dateString = formatter.format(currentTime_1);
return dateString;
}
public String getDayDate() {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date currentTime_1 = new java.util.Date();
String dateString = formatter.format(currentTime_1);
return dateString;
}
public String getNowWeek() {
String Targetstr = GetStringDate();
String tempStrI[] = new String[3];
tempStrI[0] = Targetstr.substring(0, 4);
tempStrI[1] = Targetstr.substring(5, 7);
tempStrI[2] = Targetstr.substring(8, 10);
Calendar cal = Calendar.getInstance();
cal.set(
Integer.parseInt(tempStrI[0]) - 1900,
Integer.parseInt(tempStrI[1]),
Integer.parseInt(tempStrI[2]));
int NowWeek = cal.get(Calendar.WEEK_OF_MONTH);
cal.add(Calendar.DAY_OF_YEAR, -NowWeek);
int stryear = cal.get(Calendar.YEAR);
int strmonth = cal.get(Calendar.MONTH);
int strday = cal.get(Calendar.DAY_OF_MONTH);
return new String(
"" + (stryear + 1900) + "-" + strmonth + "-" + strday);
}
public String getLastNumWeek(int LastNum) {
String Targetstr = GetStringDate();
String tempStrI[] = new String[3];
tempStrI[0] = Targetstr.substring(0, 4);
tempStrI[1] = Targetstr.substring(5, 7);
tempStrI[2] = Targetstr.substring(8, 10);
Calendar cal = Calendar.getInstance();
cal.set(
Integer.parseInt(tempStrI[0]) - 1900,
Integer.parseInt(tempStrI[1]),
Integer.parseInt(tempStrI[2]));
int NowWeek = cal.get(Calendar.WEEK_OF_MONTH);
cal.add(Calendar.WEEK_OF_MONTH, -LastNum);
cal.add(Calendar.DAY_OF_YEAR, -NowWeek);
int stryear = cal.get(Calendar.YEAR);
int strmonth = cal.get(Calendar.MONTH);
int strday = cal.get(Calendar.DAY_OF_MONTH);
return new String(
"" + (stryear + 1900) + "-" + strmonth + "-" + strday);
}
public String getNowDay() {
String Targetstr = GetStringDate();
String tempStrI[] = new String[3];
tempStrI[0] = Targetstr.substring(0, 4);
tempStrI[1] = Targetstr.substring(5, 7);
tempStrI[2] = Targetstr.substring(8, 10);
return new String(
"" + tempStrI[0] + "-" + tempStrI[1] + "-" + tempStrI[2]);
}
public String getLastNumDay(int LastNum) {
String Targetstr = GetStringDate();
String tempStrI[] = new String[3];
tempStrI[0] = Targetstr.substring(0, 4);
tempStrI[1] = Targetstr.substring(5, 7);
tempStrI[2] = Targetstr.substring(8, 10);
Calendar cal = Calendar.getInstance();
cal.set(
Integer.parseInt(tempStrI[0]) - 1900,
Integer.parseInt(tempStrI[1]),
Integer.parseInt(tempStrI[2]));
cal.add(Calendar.DAY_OF_YEAR, -LastNum);
int stryear = cal.get(Calendar.YEAR);
int strmonth = cal.get(Calendar.MONTH);
int strday = cal.get(Calendar.DAY_OF_MONTH);
return new String(
"" + (stryear + 1900) + "-" + strmonth + "-" + strday);
}
public String getNowMonth() {
String Targetstr = GetStringDate();
String tempStrI[] = new String[3];
tempStrI[0] = Targetstr.substring(0, 4);
tempStrI[1] = Targetstr.substring(5, 7);
tempStrI[2] = Targetstr.substring(8, 10);
Calendar cal = Calendar.getInstance();
cal.set(
Integer.parseInt(tempStrI[0]) - 1900,
Integer.parseInt(tempStrI[1]),
Integer.parseInt(tempStrI[2]));
int stryear = cal.get(Calendar.YEAR);
int strmonth = cal.get(Calendar.MONTH);
return new String("" + (stryear + 1900) + "-" + strmonth + "-" + 1);
}
public String getLastNumMonth(int LastNum) {
String Targetstr = GetStringDate();
String tempStrI[] = new String[3];
tempStrI[0] = Targetstr.substring(0, 4);
tempStrI[1] = Targetstr.substring(5, 7);
tempStrI[2] = Targetstr.substring(8, 10);
Calendar cal = Calendar.getInstance();
cal.set(
Integer.parseInt(tempStrI[0]) - 1900,
Integer.parseInt(tempStrI[1]),
Integer.parseInt(tempStrI[2]));
cal.add(Calendar.MONTH, -1);
int stryear = cal.get(Calendar.YEAR);
int strmonth = cal.get(Calendar.MONTH);
int strday = cal.get(Calendar.DAY_OF_MONTH);
return new String("" + (stryear + 1900) + "-" + strmonth + "-" + 1);
}
public String getNowYear() {
String Targetstr = GetStringDate();
String tempStrI[] = new String[3];
tempStrI[0] = Targetstr.substring(0, 4);
tempStrI[1] = Targetstr.substring(5, 7);
tempStrI[2] = Targetstr.substring(8, 10);
Calendar cal = Calendar.getInstance();
cal.set(
Integer.parseInt(tempStrI[0]) - 1900,
Integer.parseInt(tempStrI[1]),
Integer.parseInt(tempStrI[2]));
int stryear = cal.get(Calendar.YEAR);
return new String("" + (stryear + 1900) + "-" + 1 + "-" + 1);
}
public String getNowYear(int LastNum) {
String Targetstr = GetStringDate();
String tempStrI[] = new String[3];
tempStrI[0] = Targetstr.substring(0, 4);
tempStrI[1] = Targetstr.substring(5, 7);
tempStrI[2] = Targetstr.substring(8, 10);
Calendar cal = Calendar.getInstance();
cal.set(
Integer.parseInt(tempStrI[0]) - 1900,
Integer.parseInt(tempStrI[1]),
Integer.parseInt(tempStrI[2]));
cal.add(Calendar.YEAR, -1);
int stryear = cal.get(Calendar.YEAR);
return new String("" + (stryear + 1900) + "-" + 1 + "-" + 1);
}
public String ChangeDate(
String Targetstr,
String year,
String month,
String day) {
String tempStrI[] = new String[3];
tempStrI[0] = Targetstr.substring(0, 4);
tempStrI[1] = Targetstr.substring(5, 7);
tempStrI[2] = Targetstr.substring(8, 10);
Calendar cal = Calendar.getInstance();
int temp = 0;
cal.set(
Integer.parseInt(tempStrI[0]) - 1900,
Integer.parseInt(tempStrI[1]),
Integer.parseInt(tempStrI[2]));
if ((temp = year.indexOf('-')) > 0)
year = year.substring(temp);
if ((temp = month.indexOf('-')) > 0)
month = month.substring(temp);
if ((temp = day.indexOf('-')) > 0)
day = day.substring(temp);
cal.add(Calendar.YEAR, Integer.parseInt(year));
cal.add(Calendar.MONTH, Integer.parseInt(month));
cal.add(Calendar.DAY_OF_MONTH, Integer.parseInt(day));
int stryear = cal.get(Calendar.YEAR);
int strmonth = cal.get(Calendar.MONTH);
int strday = cal.get(Calendar.DAY_OF_MONTH);
stryear += 1900;
return new String("" + stryear + "-" + strmonth + "-" + strday);
}
public String CompareDate(String SourceDate, String TargetDate) {
String newSource = "";
int tempI = 0;
int tempII = 0;
String newTarget = "";
tempI = SourceDate.indexOf(' ');
tempII = TargetDate.indexOf(' ');
if (tempI > 0 && tempII > 0) {
newSource = SourceDate.substring(0, tempI);
newTarget = TargetDate.substring(0, tempII);
newSource = newSource.trim();
newTarget = newTarget.trim();
} else {
newSource = SourceDate;
newTarget = TargetDate;
}
if (newSource.equals("") || newTarget.equals(""))
return null;
else {
String tempStrI[] = new String[3];
String tempStrII[] = new String[3];
int tempIII = 0;
int tempIIII = 0;
int temp = 0;
tempStrI[0] = newSource.substring(0, 4);
tempStrI[1] = newSource.substring(5, 7);
tempStrI[2] = newSource.substring(8, 10);
tempStrII[0] = newTarget.substring(0, 4);
tempStrII[1] = newTarget.substring(5, 7);
tempStrII[2] = newTarget.substring(8, 10);
tempIII = Integer.parseInt(tempStrI[0]);
tempIIII = Integer.parseInt(tempStrII[0]);
if (tempIII != tempIIII) {
temp = tempIII - tempIII;
temp = temp * 360;
}
tempIII = Integer.parseInt(tempStrI[1]);
tempIIII = Integer.parseInt(tempStrII[1]);
if (tempIII != tempIIII) {
temp = temp + (tempIII - tempIIII) * 30;
}
tempIII = Integer.parseInt(tempStrI[2]);
tempIIII = Integer.parseInt(tempStrII[2]);
if (tempIII != tempIIII) {
temp = temp + (tempIII - tempIIII);
}
if (temp < 0) {
temp = 0 - temp;
return new String("" + temp);
} else
return new String("" + temp);
}
}
}
Tags:Calendar,例子

