獲取當前時間的年月日方法web
Calendar now = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd hhmmss");
spa
Date d = new Date();.net
String time = format.format(d); //獲取當前日期
orm
String time = format.format(now.getTime()); //獲取當前日期
註釋: now.get(Calendar.YEAR)/now.get(Calendar.MONTH) + 1)/now.get(Calendar.DAY_OF_MONTH)...//獲取當前年月日
字符串
經常使用日期方法get
privateSimpleDateFormat sf = null;
/*獲取系統時間 格式爲:"yyyy/MM/dd "*/
public static String getCurrentDate() {
Date d = newDate();
sf = newSimpleDateFormat("yyyy年MM月dd日");
returnsf.format(d);
}
/*時間戳轉換成字符竄*/
public static String getDateToString(long time) {
Date d = newDate(time);
sf = newSimpleDateFormat("yyyy年MM月dd日");
returnsf.format(d);
}
/*將字符串轉爲時間戳*/
public static long getStringToDate(String time) {
sdf = newSimpleDateFormat("yyyy年MM月dd日");
Date date = newDate();
try{
date = sdf.parse(time);
} catch(ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
returndate.getTime();
}it
/**
* 獲取該月的第一天和最後一天
* @param pos 0當前月 1 下個月 -1 上個月
*/
public void showMonthDate(int pos) {
if(pos ==0){ //本月
curMonthCalender = Calendar.getInstance();
}
else if(pos ==1){ //下個月
curMonthCalender.add(Calendar.MONTH,1);
}
else { //上個月
curMonthCalender.add(Calendar.MONTH,-1);
}
curMonthCalender.set(Calendar.DATE,1);//獲得該月第一天
String startday = formatter.format(curMonthCalender.getTime());
curMonthCalender.roll(Calendar.DATE, -1);////獲得該月最後一天
String endday = formatter.format(curMonthCalender.getTime());
timeStr = startday + "至" + endday;
tvTime.setText(timeStr);
}
/**
* 獲取該年日期
* @param pos 0當前年 1 下一年 -1 上一年io
*/
public void showYearDate(int pos) {
if(pos ==0){ //本年
Calendar curYearCalendar = Calendar.getInstance();
curYear = curYearCalendar.get(Calendar.YEAR);
}
else if(pos ==1){ //下個年
curYear +=1;
}
else { //上個年
curYear -=1;
}
String startday = curYear + "-01-01";
String endday = curYear + "-12-31";
timeStr = startday + "至" + endday;
tvTime.setText(timeStr);
}
/**
* 獲取該季度的第一天和最後一天
* @param pos 0當前季度 1 下個季度 -1 上個季度
*/
public void showQuaterDate(int pos) {
if(pos ==0){ //本季度
curQuaterCalender = Calendar.getInstance();
int month = curQuaterCalender.MONTH;
int stMonth =0;
if(month >=0 && month<=3){
stMonth =0;
}
else if(month >=4 && month<=8){
stMonth =4;
}
else{
stMonth =9;
}
curQuaterCalender.set(Calendar.MONTH, stMonth);
}
else if(pos ==1){ //下季度
curQuaterCalender.add(Calendar.MONTH,3);
}
else { //上季度
curQuaterCalender.add(Calendar.MONTH,-3);
}
curQuaterCalender.set(Calendar.DATE,1);//獲得該月第一天
String startday = formatter.format(curQuaterCalender.getTime());
Calendar tempDate = Calendar.getInstance();
tempDate.set(Calendar.YEAR, curQuaterCalender.get(Calendar.YEAR));
tempDate.set(Calendar.MONTH, curQuaterCalender.get(Calendar.MONTH)+3);
tempDate.set(Calendar.DAY_OF_MONTH, curQuaterCalender.get(Calendar.DAY_OF_MONTH));
tempDate.roll(Calendar.DATE, -1);
String endday = formatter.format(tempDate.getTime());
timeStr = startday + "至" + endday;
tvTime.setText(timeStr);
}form