/**
两天前日期(yyyy-MM-dd)
@return
*/
public static String getYesterdayDate(){
Date date=new Date();
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
calendar.add(Calendar.DATE,-2);
date=calendar.getTime();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String d = formatter.format(date);
return d;
}
/**
获取每月一号日期(yyyy-MM-dd)
@return
*/
public static String getFirstDayOfMonth(){
Calendar c = Calendar.getInstance();
c.add(Calendar.MONTH, 0);
c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String d = formatter.format(c.getTime());
return d;
}
/**
获取某一日期的一周起始日期和结束日期(即:周一和周日的日期)
@throws ParseException
*/
public static Map getWeek(String date) throws ParseException{
Map map=new HashMap();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); //设置时间格式
Calendar cal = Calendar.getInstance();
Date time=sdf.parse(date);
cal.setTime(time);
System.out.println("要计算日期为:"+sdf.format(cal.getTime())); //输出要计算日期
//判断要计算的日期是否是周日,如果是则减一天计算周六的,否则会出问题,计算到下一周去了
int dayWeek = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
if(1 == dayWeek) {
cal.add(Calendar.DAY_OF_MONTH, -1);
}
cal.setFirstDayOfWeek(Calendar.MONDAY);//设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
int day = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
cal.add(Calendar.DATE, cal.getFirstDayOfWeek()-day);//根据日历的规则,给当前日期减去星期几与一个星期第一天的差值
String startDay=sdf.format(cal.getTime());
cal.add(Calendar.DATE, 6);
String endDay=sdf.format(cal.getTime());
map.put("startDay", startDay);
map.put("endDay", endDay);
System.out.println(map);
return map;
}
public static void main(String [] args) throws ParseException{
getWeek("2016-04-24");
}
/**
获取某一日期是周几
@throws ParseException
*/
public static String getWeekOfDate(Date dt) throws ParseException {
String[] weekDays = {"Sun","Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
Calendar cal = Calendar.getInstance();
cal.setTime(dt);
int w = cal.get(Calendar.DAY_OF_WEEK)-1;
if (w < 0)
w = 0;
return weekDays[w];
}
/**
将date(2016-12-02)转换为(12.2)
@throws ParseException
*/
public static String getDay(String date) throws ParseException{
String s=Integer.parseInt(date.substring(5,7))+"."+Integer.parseInt(date.substring(8,10));
return s;
}
/**
将小数转为百分数(99.2%)
@throws ParseException
*/
public static String getProportion(int a,int b){
String pro="";
if(a!=0&&b!=0){
double percent = (double)a/b;
//获取格式化对象
NumberFormat nt = NumberFormat.getPercentInstance();
//设置百分数精确度1即保留两位小数
nt.setMinimumFractionDigits(1);
//最后格式化并输出
pro=nt.format(percent);
}
return pro;
}
/**
将小数转为百分数(+/-7.5%)
@throws ParseException
*/
public static String getProportionSign(int a,int b){
String pro="";
if(a!=0&&b!=0){
double percent = (double)a/b;
//获取格式化对象
NumberFormat nt = NumberFormat.getPercentInstance();
//设置百分数精确度1即保留两位小数
nt.setMinimumFractionDigits(1);
//最后格式化并输出
if(percent>1){
pro="+"+nt.format(percent-1);
}else{
pro=nt.format(percent-1);
}
}
return pro;
}