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;
}
}