W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
/**
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;
}
/**
public static String getFirstDayOfMonth(){
Calendar c = Calendar.getInstance();
c.add(Calendar.MONTH, 0);
c.set(Calendar.DAY_OF_MONTH,1);//設(shè)置為1號,當前日期既為本月第一天
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String d = formatter.format(c.getTime());
return d;
}
/**
public static Map<String,String> getWeek(String date) throws ParseException{
Map<String,String> map=new HashMap<String,String>();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); //設(shè)置時間格式
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);//設(shè)置一個星期的第一天,按中國的習(xí)慣一個星期的第一天是星期一
int day = cal.get(Calendar.DAY_OF_WEEK);//獲得當前日期是一個星期的第幾天
cal.add(Calendar.DATE, cal.getFirstDayOfWeek()-day);//根據(jù)日歷的規(guī)則,給當前日期減去星期幾與一個星期第一天的差值
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");
}
/**
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];
}
/**
public static String getDay(String date) throws ParseException{
String s=Integer.parseInt(date.substring(5,7))+"."+Integer.parseInt(date.substring(8,10));
return s;
}
/**
public static String getProportion(int a,int b){
String pro="";
if(a!=0&&b!=0){
double percent = (double)a/b;
//獲取格式化對象
NumberFormat nt = NumberFormat.getPercentInstance();
//設(shè)置百分數(shù)精確度1即保留兩位小數(shù)
nt.setMinimumFractionDigits(1);
//最后格式化并輸出
pro=nt.format(percent);
}
return pro;
}
/**
public static String getProportionSign(int a,int b){
String pro="";
if(a!=0&&b!=0){
double percent = (double)a/b;
//獲取格式化對象
NumberFormat nt = NumberFormat.getPercentInstance();
//設(shè)置百分數(shù)精確度1即保留兩位小數(shù)
nt.setMinimumFractionDigits(1);
//最后格式化并輸出
if(percent>1){
pro="+"+nt.format(percent-1);
}else{
pro=nt.format(percent-1);
}
}
return pro;
}
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: