如题,补充一点,是为了求当前这一个月的数据的!
如果没有函数的话,我可能构造!
Date yes=new Date();
SimpleDateFormat sdf_y = new SimpleDateFormat("yyyy-MM");
Timestamp yes_from = Timestamp.valueOf(sdf_y.format(yes)+"-01 00:00:00");
Timestamp yes_to = Timestamp.valueOf(sdf_y.format(yes)+"-31 23:59:59");但这样遇到一个月只有28,29,30天的就不行了!
求帮忙
如果没有函数的话,我可能构造!
Date yes=new Date();
SimpleDateFormat sdf_y = new SimpleDateFormat("yyyy-MM");
Timestamp yes_from = Timestamp.valueOf(sdf_y.format(yes)+"-01 00:00:00");
Timestamp yes_to = Timestamp.valueOf(sdf_y.format(yes)+"-31 23:59:59");但这样遇到一个月只有28,29,30天的就不行了!
求帮忙
* 取得当前月第一天
* @param date
* @return
* Date: 2012-5-14下午03:53:37
*/
public static Date getMonthFirst(Date date) {
try {
SimpleDateFormat sdfShort = new SimpleDateFormat(DATE_FORMAT_SHORT);
SimpleDateFormat sdfYear = new SimpleDateFormat("yyyy");
SimpleDateFormat sdfMonth = new SimpleDateFormat("MM");
return sdfShort.parse(sdfYear.format(date) + "-" + sdfMonth.format(date) + "-01");
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/**
* 取得当前月最后一天
* @param date
* @return
* Date: 2012-5-14下午03:53:57
*/
public static Date getMonthLast(Date date) {
try {
SimpleDateFormat sdfShort = new SimpleDateFormat(DATE_FORMAT_SHORT);
SimpleDateFormat sdfYear = new SimpleDateFormat("yyyy");
SimpleDateFormat sdfMonth = new SimpleDateFormat("MM");
int year = Integer.valueOf(sdfYear.format(date));
int month = Integer.valueOf(sdfMonth.format(date));
int day = getMonthDays(year, month);
return sdfShort.parse(year + "-" + month + "-" + day);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
* 取得某月天数
* @param year
* @param month
* @return
* Date: 2012-5-14下午04:13:41
*/
public static int getMonthDays(int year, int month) {
Calendar a = Calendar.getInstance();
a.set(Calendar.YEAR, year);
a.set(Calendar.MONTH, month - 1);
a.set(Calendar.DATE, 1);//把日期设置为当月第一天
a.roll(Calendar.DATE, -1);//日期回滚一天,也就是最后一天
int maxDate = a.get(Calendar.DATE);
return maxDate;
}
java.util.Calendar 这个类 看了之后有关时间的问题 就问题不大了
时间戳 date 时间串 Calendar 这几个了解一相互的转换
package test;import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;public class TestTime {
/**
* 获取当月的最后一天
* @return
*/
public static int getCurrentMon4LastDay(){
Calendar c=Calendar.getInstance();
c.roll(Calendar.MONTH,1);
c.set(Calendar.DATE,1);
c.add(Calendar.DATE,-1);
return c.get(Calendar.DATE);
}
/**
* 获取两个日期的差,单位:毫秒。
* @param args
*/
public static long getDateDiff(Date d1,Date d2){
long millionSecond1=d1.getTime();
long millionSecond2=d2.getTime();
return millionSecond1>millionSecond2?millionSecond1-millionSecond2:millionSecond2-millionSecond1;
}
public static void main(String[] args){
Date yes=new Date();
SimpleDateFormat sdf_y = new SimpleDateFormat("yyyy-MM");
Timestamp yes_from = Timestamp.valueOf(sdf_y.format(yes)+"-01 00:00:00");
int currentMon4LastDay=getCurrentMon4LastDay();
Timestamp yes_to = Timestamp.valueOf(sdf_y.format(yes)+"-"+currentMon4LastDay+" 23:59:59");
long diff=getDateDiff(yes_from,yes_to);
//输出
System.out.println(yes_from);
System.out.println(yes_to);
System.out.println(diff);
}}