欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

工具类

程序员文章站 2022-05-19 15:43:00
...

public static int ceil(int a,int b){
double t1=a;
double t2=b;
int c=(int) Math.ceil(t1/t2);
return c;
}

ceil()方法执行的是向上取整计算,它返回的是大于或等于函数参数,并且与之最接近的整数

3,2---->2



转成负数
public static int zhuanfu(int i){
if(i>0){
String str=String.valueOf(i);
str="-"+str;
return Integer.valueOf(str);
}else return i;
}



获取现在时间
public static String getNowTime(){
Date date = new Date(System.currentTimeMillis());
return FastDateFormat.getInstance("yyyyMMddHHmmss").format(date);
}




1-10之间随机取6位数
public static String getRandom(long l){
char[] randoms = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
Random random = new Random(l);
StringBuffer ret = new StringBuffer();
for (int i = 0; i < 6; i++) {
ret.append(randoms[random.nextInt(randoms.length)]);
}
random = null;
return ret.toString();
}



获取年份
public static int getYear(Date date){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.YEAR);
}



获取星期数
public static int getWeekNumber(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setFirstDayOfWeek(Calendar.MONDAY);

calendar.setTime(date);
return calendar.get(Calendar.WEEK_OF_YEAR);
}



public static Date getFirstDayOfWeek(Date date) {
Calendar c = new GregorianCalendar();
c.setFirstDayOfWeek(Calendar.MONDAY);
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Monday
return c.getTime();
}

public static Date getLastDayOfWeek(Date date) {
Calendar c = new GregorianCalendar();
c.setFirstDayOfWeek(Calendar.MONDAY);
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); // Sunday
return c.getTime();
}