工作中积累的日期、数字处理方法
来源:互联网 发布:淘宝积分兑换优酷会员 编辑:程序博客网 时间:2024/05/16 23:51
package xxxxxxx
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Set;
import java.util.TimeZone;
public class *****Util {
public static final int MAXDIFFERDAY=360;
public static final SimpleDateFormat FORMAT=new SimpleDateFormat("yyyy-MM-dd");
private static final String DEFAULT_PATTERN = "yyyyMMddHHmmss";
public static final String PRECISE_DAY_PATTERN = "yyyy-MM-dd";
public static final String YEAR_MONTH_PATTERN = "yyyy-MM";
public static final String MONTH_DAY_PATTERN = "MM-dd";
public static final String YEAR_PATTERN = "yyyy";
public static final String MONTH_PATTERN = "MM";
public static final String DAY_PATTERN = "dd";
public static final int DEFAULT_DIV_SCALE = 3;
public static Date getMinDate() throws ParseException{
return FORMAT.parse("2000-01-01");
}
public static Date getMaxDate() throws ParseException{
return FORMAT.parse("2099-12-31");
}
public static Date getCurrentDate(){
Date date = new Date();
return date;
}
public static Date getNextDay(Date date){
Calendar cal=Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, 1);
return cal.getTime();
}
public static Date getNextDay(Date date,int i){
Calendar cal=Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, 1*i);
return cal.getTime();
}
// 获取下一周的日期
public static Date getNextWeek(Date date){
Calendar cal=Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, 7);
return cal.getTime();
}
// 获取多周后的日期
public static Date getNextWeek(Date date, int i){
Calendar cal=Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, 7*i);
return cal.getTime();
}
// 获取下一月的日期
public static Date nextMonth(Date currentDate) {
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(currentDate);
cal.add(GregorianCalendar.MONTH, 1);// 在月份上加1
return cal.getTime();
}
// 获取整月后的日期
public static Date nextMonth(Date currentDate, int addmonth) {
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(currentDate);
cal.add(GregorianCalendar.MONTH, addmonth);// 在月份上加
return cal.getTime();
}
// 获取下一年的日期
public static Date nextYear(Date currentDate) {
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(currentDate);
cal.add(GregorianCalendar.YEAR, 1);// 在年上加1
return cal.getTime();
}
// 获取下一年的日期
public static Date nextYear(Date currentDate, int i) {
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(currentDate);
cal.add(GregorianCalendar.YEAR, 1*i);// 在年上加1
return cal.getTime();
}
//获取某天所在周的周一
public static Date getFirstDayOfWeek(Date date){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
//if Sunday then get previous day. in Calendar Sunday is the first day of week
if(calendar.get(Calendar.DAY_OF_WEEK)==1){
calendar.setTime(ToolingUtil.getNextDay(date,-1));
}
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
return calendar.getTime();
}
//获取某天所在月的第一天
public static Date getMonthFirstDay(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_MONTH, 1);
return calendar.getTime();
}
//获取当前月的第一天
public static Date getCurrentMonthFirstDay() {
Calendar calendar = Calendar.getInstance();
Date date = calendar.getTime();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_MONTH, 1);
return calendar.getTime();
}
public static Integer date2Integer(Date time){
String strDate = date2str(time,PRECISE_DAY_PATTERN);
strDate = strDate.replace("-", "");
Integer result = Integer.valueOf(strDate);
return result;
}
public static Date Integer2date(Integer intdate){
String strDate = intdate.toString();
String strDate2 = strDate.substring(0, 4)+"-"+strDate.substring(4, 6)+"-"+strDate.substring(6, strDate.length());
return str2date(strDate2,PRECISE_DAY_PATTERN);
}
public static String date2str(Date time, String pattern) {
if (time == null) {
throw new IllegalArgumentException("Date is null");
}
if (pattern != null && !"".equals(pattern)) {
if (!"yyyyMMddHHmmss".equals(pattern)
&& !"yyyy-MM-dd HH:mm:ss".equals(pattern)
&& !"yyyy-MM-dd".equals(pattern)
&& !"yyyy-MM".equals(pattern)
&& !"MM-dd".equals(pattern)
&& !"MM/dd/yyyy".equals(pattern)){
throw new IllegalArgumentException("Date format ["+pattern+"] is invalid");
}
}else{
pattern = DEFAULT_PATTERN;
}
Calendar cal = Calendar.getInstance(TimeZone.getDefault());
cal.setTime(time);
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.format(cal.getTime());
}
public static Date str2date(String str, String pattern){
if(str == null){
throw new IllegalArgumentException("Date is null");
}
if (pattern != null && !"".equals(pattern)) {
if (!"yyyyMMddHHmmss".equals(pattern)
&& !"yyyy-MM-dd HH:mm:ss".equals(pattern)
&& !"yyyy-MM-dd".equals(pattern)
&& !"MM/dd/yyyy".equals(pattern)){
throw new IllegalArgumentException("Date format ["+pattern+"] is invalid");
}
}else{
pattern = PRECISE_DAY_PATTERN;
}
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
Date date = null;
try {
date = sdf.parse(str);
} catch (ParseException e) {
throw new IllegalArgumentException("Date format ["+str+"] is invalid.");
}
return date;
}
public static int calculateWorkingDays(Date beginDate, Date endDate,Set<String> escapeNonWorkingDay) {
if (beginDate == null || endDate == null || beginDate.after(endDate)) {
return 0;
}
int workingDays = 0;
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(beginDate);
long time = endDate.getTime() - beginDate.getTime();
time = Math.abs(time);
long day = time / 3600000 / 24 + 1;
for (int i = 0; i < day; i++) {
if(escapeNonWorkingDay==null){
if (gc.get(GregorianCalendar.DAY_OF_WEEK) != GregorianCalendar.SATURDAY
&& gc.get(GregorianCalendar.DAY_OF_WEEK) != GregorianCalendar.SUNDAY) {
workingDays++;
}
}else{
if(!escapeNonWorkingDay.contains(FORMAT.format(gc.getTime()))){
workingDays++;
}
}
// Add one day
gc.add(GregorianCalendar.DATE, 1);
}
return workingDays;
}
public static int compareDate(Date first, Date second){
Calendar cal1=Calendar.getInstance();
cal1.setTime(first);
Calendar cal2=Calendar.getInstance();
cal2.setTime(second);
if(cal1.get(Calendar.YEAR)>cal2.get(Calendar.YEAR)){
return 1;
}else if(cal1.get(Calendar.YEAR)<cal2.get(Calendar.YEAR)){
return -1;
}else if((cal1.get(Calendar.MONTH)+1)>(cal2.get(Calendar.MONTH)+1)){
return 1;
}else if((cal1.get(Calendar.MONTH)+1)<(cal2.get(Calendar.MONTH)+1)){
return -1;
}else if(cal1.get(Calendar.DAY_OF_MONTH)>cal2.get(Calendar.DAY_OF_MONTH)){
return 1;
}else if(cal1.get(Calendar.DAY_OF_MONTH)<cal2.get(Calendar.DAY_OF_MONTH)){
return -1;
}else{
return 0;
}
}
public static int compareYearMonth(Date first, Date second){
Calendar cal1=Calendar.getInstance();
cal1.setTime(first);
Calendar cal2=Calendar.getInstance();
cal2.setTime(second);
if(cal1.get(Calendar.YEAR)>cal2.get(Calendar.YEAR)){
return 1;
}else if(cal1.get(Calendar.YEAR)<cal2.get(Calendar.YEAR)){
return -1;
}else if((cal1.get(Calendar.MONTH)+1)>(cal2.get(Calendar.MONTH)+1)){
return 1;
}else if((cal1.get(Calendar.MONTH)+1)<(cal2.get(Calendar.MONTH)+1)){
return -1;
}else{
return 0;
}
}
/**
* get 相差天数
* @param startDate
* @param endDate
* @return
*/
public static long getDistDates(Date startDate,Date endDate)
{
long totalDate = 0;
Calendar calendar = Calendar.getInstance();
calendar.setTime(startDate);
long timestart = calendar.getTimeInMillis();
calendar.setTime(endDate);
long timeend = calendar.getTimeInMillis();
totalDate = Math.abs((timeend - timestart))/(1000*60*60*24);
return totalDate;
}
/**
* 相除
* @param v1
* @param v2
* @return double
*/
public static double divide(double v1, double v2)
{
return divide(v1, v2, DEFAULT_DIV_SCALE);
}
public static double divide(double v1, double v2, int scale)
{
return divide(v1, v2, scale, BigDecimal.ROUND_HALF_EVEN);
}
public static double divide(double v1, double v2, int scale, int round_mode) {
if (scale < 0)
{
throw new IllegalArgumentException(
"The scale must be a positive integer or zero");
}
BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
return b1.divide(b2, scale, round_mode).doubleValue();
}
/**
* 去掉小数最后面的0
* @param strdecimal
* @return
*/
public static String formatStringDecimal(String strdecimal){
if(strdecimal.indexOf(".") > 0){
strdecimal = strdecimal.replaceAll("0+?$", "");//去掉多余的0
strdecimal = strdecimal.replaceAll("[.]$", "");//如最后一位是.则去掉
}
return strdecimal;
}
public static double getFixfloat(double d){
BigDecimal bg = new BigDecimal(d);
double f1 = bg.setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue();
return f1;
}
/**
* 只取整数部分,不进行四舍五入
* @param d
* @return
*/
public static int getInteger(Double d){
DecimalFormat df = new DecimalFormat("0.00");
String str = df.format(d)+"";
int i = str.indexOf(".");
str = str.substring(0, i);
return Integer.parseInt(str);
}
/**
* 精确对比两个数字
* @param v1 需要被对比的第一个数
* @param v2 需要被对比的第二个数
* @return 如果两个数一样则返回0,如果第一个数比第二个数大则返回1,反之返回-1
*/
public static int compareTo(double v1,double v2){
BigDecimal b1 = new BigDecimal(v1);
BigDecimal b2 = new BigDecimal(v2);
return b1.compareTo(b2);
}
}
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Set;
import java.util.TimeZone;
public class *****Util {
public static final int MAXDIFFERDAY=360;
public static final SimpleDateFormat FORMAT=new SimpleDateFormat("yyyy-MM-dd");
private static final String DEFAULT_PATTERN = "yyyyMMddHHmmss";
public static final String PRECISE_DAY_PATTERN = "yyyy-MM-dd";
public static final String YEAR_MONTH_PATTERN = "yyyy-MM";
public static final String MONTH_DAY_PATTERN = "MM-dd";
public static final String YEAR_PATTERN = "yyyy";
public static final String MONTH_PATTERN = "MM";
public static final String DAY_PATTERN = "dd";
public static final int DEFAULT_DIV_SCALE = 3;
public static Date getMinDate() throws ParseException{
return FORMAT.parse("2000-01-01");
}
public static Date getMaxDate() throws ParseException{
return FORMAT.parse("2099-12-31");
}
public static Date getCurrentDate(){
Date date = new Date();
return date;
}
public static Date getNextDay(Date date){
Calendar cal=Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, 1);
return cal.getTime();
}
public static Date getNextDay(Date date,int i){
Calendar cal=Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, 1*i);
return cal.getTime();
}
// 获取下一周的日期
public static Date getNextWeek(Date date){
Calendar cal=Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, 7);
return cal.getTime();
}
// 获取多周后的日期
public static Date getNextWeek(Date date, int i){
Calendar cal=Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, 7*i);
return cal.getTime();
}
// 获取下一月的日期
public static Date nextMonth(Date currentDate) {
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(currentDate);
cal.add(GregorianCalendar.MONTH, 1);// 在月份上加1
return cal.getTime();
}
// 获取整月后的日期
public static Date nextMonth(Date currentDate, int addmonth) {
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(currentDate);
cal.add(GregorianCalendar.MONTH, addmonth);// 在月份上加
return cal.getTime();
}
// 获取下一年的日期
public static Date nextYear(Date currentDate) {
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(currentDate);
cal.add(GregorianCalendar.YEAR, 1);// 在年上加1
return cal.getTime();
}
// 获取下一年的日期
public static Date nextYear(Date currentDate, int i) {
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(currentDate);
cal.add(GregorianCalendar.YEAR, 1*i);// 在年上加1
return cal.getTime();
}
//获取某天所在周的周一
public static Date getFirstDayOfWeek(Date date){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
//if Sunday then get previous day. in Calendar Sunday is the first day of week
if(calendar.get(Calendar.DAY_OF_WEEK)==1){
calendar.setTime(ToolingUtil.getNextDay(date,-1));
}
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
return calendar.getTime();
}
//获取某天所在月的第一天
public static Date getMonthFirstDay(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_MONTH, 1);
return calendar.getTime();
}
//获取当前月的第一天
public static Date getCurrentMonthFirstDay() {
Calendar calendar = Calendar.getInstance();
Date date = calendar.getTime();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_MONTH, 1);
return calendar.getTime();
}
public static Integer date2Integer(Date time){
String strDate = date2str(time,PRECISE_DAY_PATTERN);
strDate = strDate.replace("-", "");
Integer result = Integer.valueOf(strDate);
return result;
}
public static Date Integer2date(Integer intdate){
String strDate = intdate.toString();
String strDate2 = strDate.substring(0, 4)+"-"+strDate.substring(4, 6)+"-"+strDate.substring(6, strDate.length());
return str2date(strDate2,PRECISE_DAY_PATTERN);
}
public static String date2str(Date time, String pattern) {
if (time == null) {
throw new IllegalArgumentException("Date is null");
}
if (pattern != null && !"".equals(pattern)) {
if (!"yyyyMMddHHmmss".equals(pattern)
&& !"yyyy-MM-dd HH:mm:ss".equals(pattern)
&& !"yyyy-MM-dd".equals(pattern)
&& !"yyyy-MM".equals(pattern)
&& !"MM-dd".equals(pattern)
&& !"MM/dd/yyyy".equals(pattern)){
throw new IllegalArgumentException("Date format ["+pattern+"] is invalid");
}
}else{
pattern = DEFAULT_PATTERN;
}
Calendar cal = Calendar.getInstance(TimeZone.getDefault());
cal.setTime(time);
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.format(cal.getTime());
}
public static Date str2date(String str, String pattern){
if(str == null){
throw new IllegalArgumentException("Date is null");
}
if (pattern != null && !"".equals(pattern)) {
if (!"yyyyMMddHHmmss".equals(pattern)
&& !"yyyy-MM-dd HH:mm:ss".equals(pattern)
&& !"yyyy-MM-dd".equals(pattern)
&& !"MM/dd/yyyy".equals(pattern)){
throw new IllegalArgumentException("Date format ["+pattern+"] is invalid");
}
}else{
pattern = PRECISE_DAY_PATTERN;
}
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
Date date = null;
try {
date = sdf.parse(str);
} catch (ParseException e) {
throw new IllegalArgumentException("Date format ["+str+"] is invalid.");
}
return date;
}
public static int calculateWorkingDays(Date beginDate, Date endDate,Set<String> escapeNonWorkingDay) {
if (beginDate == null || endDate == null || beginDate.after(endDate)) {
return 0;
}
int workingDays = 0;
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(beginDate);
long time = endDate.getTime() - beginDate.getTime();
time = Math.abs(time);
long day = time / 3600000 / 24 + 1;
for (int i = 0; i < day; i++) {
if(escapeNonWorkingDay==null){
if (gc.get(GregorianCalendar.DAY_OF_WEEK) != GregorianCalendar.SATURDAY
&& gc.get(GregorianCalendar.DAY_OF_WEEK) != GregorianCalendar.SUNDAY) {
workingDays++;
}
}else{
if(!escapeNonWorkingDay.contains(FORMAT.format(gc.getTime()))){
workingDays++;
}
}
// Add one day
gc.add(GregorianCalendar.DATE, 1);
}
return workingDays;
}
public static int compareDate(Date first, Date second){
Calendar cal1=Calendar.getInstance();
cal1.setTime(first);
Calendar cal2=Calendar.getInstance();
cal2.setTime(second);
if(cal1.get(Calendar.YEAR)>cal2.get(Calendar.YEAR)){
return 1;
}else if(cal1.get(Calendar.YEAR)<cal2.get(Calendar.YEAR)){
return -1;
}else if((cal1.get(Calendar.MONTH)+1)>(cal2.get(Calendar.MONTH)+1)){
return 1;
}else if((cal1.get(Calendar.MONTH)+1)<(cal2.get(Calendar.MONTH)+1)){
return -1;
}else if(cal1.get(Calendar.DAY_OF_MONTH)>cal2.get(Calendar.DAY_OF_MONTH)){
return 1;
}else if(cal1.get(Calendar.DAY_OF_MONTH)<cal2.get(Calendar.DAY_OF_MONTH)){
return -1;
}else{
return 0;
}
}
public static int compareYearMonth(Date first, Date second){
Calendar cal1=Calendar.getInstance();
cal1.setTime(first);
Calendar cal2=Calendar.getInstance();
cal2.setTime(second);
if(cal1.get(Calendar.YEAR)>cal2.get(Calendar.YEAR)){
return 1;
}else if(cal1.get(Calendar.YEAR)<cal2.get(Calendar.YEAR)){
return -1;
}else if((cal1.get(Calendar.MONTH)+1)>(cal2.get(Calendar.MONTH)+1)){
return 1;
}else if((cal1.get(Calendar.MONTH)+1)<(cal2.get(Calendar.MONTH)+1)){
return -1;
}else{
return 0;
}
}
/**
* get 相差天数
* @param startDate
* @param endDate
* @return
*/
public static long getDistDates(Date startDate,Date endDate)
{
long totalDate = 0;
Calendar calendar = Calendar.getInstance();
calendar.setTime(startDate);
long timestart = calendar.getTimeInMillis();
calendar.setTime(endDate);
long timeend = calendar.getTimeInMillis();
totalDate = Math.abs((timeend - timestart))/(1000*60*60*24);
return totalDate;
}
/**
* 相除
* @param v1
* @param v2
* @return double
*/
public static double divide(double v1, double v2)
{
return divide(v1, v2, DEFAULT_DIV_SCALE);
}
public static double divide(double v1, double v2, int scale)
{
return divide(v1, v2, scale, BigDecimal.ROUND_HALF_EVEN);
}
public static double divide(double v1, double v2, int scale, int round_mode) {
if (scale < 0)
{
throw new IllegalArgumentException(
"The scale must be a positive integer or zero");
}
BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
return b1.divide(b2, scale, round_mode).doubleValue();
}
/**
* 去掉小数最后面的0
* @param strdecimal
* @return
*/
public static String formatStringDecimal(String strdecimal){
if(strdecimal.indexOf(".") > 0){
strdecimal = strdecimal.replaceAll("0+?$", "");//去掉多余的0
strdecimal = strdecimal.replaceAll("[.]$", "");//如最后一位是.则去掉
}
return strdecimal;
}
public static double getFixfloat(double d){
BigDecimal bg = new BigDecimal(d);
double f1 = bg.setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue();
return f1;
}
/**
* 只取整数部分,不进行四舍五入
* @param d
* @return
*/
public static int getInteger(Double d){
DecimalFormat df = new DecimalFormat("0.00");
String str = df.format(d)+"";
int i = str.indexOf(".");
str = str.substring(0, i);
return Integer.parseInt(str);
}
/**
* 精确对比两个数字
* @param v1 需要被对比的第一个数
* @param v2 需要被对比的第二个数
* @return 如果两个数一样则返回0,如果第一个数比第二个数大则返回1,反之返回-1
*/
public static int compareTo(double v1,double v2){
BigDecimal b1 = new BigDecimal(v1);
BigDecimal b2 = new BigDecimal(v2);
return b1.compareTo(b2);
}
}
0 0
- 工作中积累的日期、数字处理方法
- 工作中积累的琐碎
- Extjs 中处理日期的方法总结
- js中对象定义状态、js判断为空的方法、js数字相加减以及日期处理
- VC中判断数字和日期的方法
- struts2中格式化输出数字和日期的方法
- WebWork/struts2中格式化输出数字和日期的方法
- struts2中格式化输出数字和日期的方法
- 日期处理的方法
- 工作中积累的一些名词解释
- 工作中积累的Linux命令
- 工作中积累的一些经验
- 工作过程中积累的书签(链接)
- 工作中积累的优秀博客
- 工作中积累的一些mssql操作
- 积累——日期处理
- xcode 中nsdate 日期的一些处理方法
- Date对象中处理时间和日期的常用方法
- 安装cocoapods管理第三方库时的问题
- JAVA 获取当前执行的函数名、当前运行的类名等等信息
- 如何通过匹配||切分字符串
- 错误 1 error C2440: “static_cast”: 无法从“void (__thiscall ClientDlg::* )
- 算法导论11.2-4未占用槽分配存储
- 工作中积累的日期、数字处理方法
- 细化-从百度识图效果对智能识图的一些思考2
- Java网络编程从入门到精通(31):非阻塞I/O简介
- Spark源码IDEA远程调试
- 安装eclipse中html/jsp/xml editor插件
- 软考路——进程与线程
- Java 基础知识课堂笔记
- Sqlstate详解
- 随堂作业(1-4)