给定日期和日期类型(年、月、周、日)返回对应日期类型的开始时间和结束时间

package com.sunwe.emview.app.test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class DateTest {
    public static void main(String[] args ) {
     Map<String,String> map=getDateTime("2015-02-02 08:42:27",3);
     System.out.println(map.get("starttime")+"------"+map.get("endtime"));
     
    }
   
    public static Map<String,String> getDateTime(String time,int datetype)
    { 
     String starttime = null;
     String endtime = null;
     Map<String,String> maptime=new HashMap<String,String>();
     Calendar cal=Calendar.getInstance();
     cal.clear();
     SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
     Date date = null;
  try {
   date = sdf.parse(time);
  } catch (ParseException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
     cal.setTime(date);
     int year=cal.get(Calendar.YEAR);
     int month=cal.get(Calendar.MONTH);
     int day_of_month=cal.get(Calendar.DAY_OF_MONTH);
     
     if(datetype==0)
     {
      cal.set(year, month, day_of_month, 0, 0, 0);
      starttime=sdf.format(cal.getTime());
      cal.set(year, month, day_of_month, 23, 59, 59);
      endtime=sdf.format(cal.getTime());
     }else if(datetype==1){
      int day=cal.get(Calendar.DAY_OF_WEEK);
      int day_of_week = day - 2;
      cal.add(Calendar.DATE, -day_of_week);
      int year1=cal.get(Calendar.YEAR);
         int month1=cal.get(Calendar.MONTH);
         int day_of_month1=cal.get(Calendar.DAY_OF_MONTH);
      cal.set(year1, month1, day_of_month1, 0, 0, 0);
      starttime=sdf.format(cal.getTime());
      cal.add(Calendar.DATE, 6);
      int year11=cal.get(Calendar.YEAR);
         int month11=cal.get(Calendar.MONTH);
         int day_of_month11=cal.get(Calendar.DAY_OF_MONTH);
         cal.set(year11, month11, day_of_month11, 23, 59, 59);
      endtime=sdf.format(cal.getTime());
     }else if(datetype==2){
      int maxday=cal.getActualMaximum(Calendar.DAY_OF_MONTH);
      cal.set(year, month, 1, 0, 0, 0);
      starttime=sdf.format(cal.getTime());
      cal.set(year, month, maxday, 23, 59, 59);
      endtime=sdf.format(cal.getTime());
     }else if(datetype==3){
      cal.set(year, 0, 1, 0, 0, 0);
      starttime=sdf.format(cal.getTime());
      cal.set(year, 11, 31, 23, 59, 59);
      endtime=sdf.format(cal.getTime());
     }
     maptime.put("starttime", starttime);
  maptime.put("endtime", endtime);
     return maptime;
     
    }  
   
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值