java 日期处理
一,日期和字符串之间转换
1, string 转换为 double 并四舍五入保留两位小数
public static double stringChangeDouble ( String str) {
double d = Double . parseDouble ( str) ;
System . out. println ( "d is " + d) ;
BigDecimal decimal = BigDecimal . valueOf ( d) ;
DecimalFormat df = new DecimalFormat ( "0.##" ) ;
df. setRoundingMode ( RoundingMode . HALF_UP) ;
String format = df. format ( decimal. doubleValue ( ) ) ;
return Double . parseDouble ( format) ;
}
2,double 转 string
public static String doubleChangeString ( double d) {
String s = String . valueOf ( d) ;
System . out. println ( "s is " + s) ;
return s;
}
3, double 保留两位小数
public static double doubleRound ( double d) {
BigDecimal decimal = BigDecimal . valueOf ( d) ;
DecimalFormat df = new DecimalFormat ( "0.##" ) ;
df. setRoundingMode ( RoundingMode . HALF_UP) ;
String format = df. format ( decimal. doubleValue ( ) ) ;
System . out. println ( Double . parseDouble ( format) + "-------返回的double--------" ) ;
return Double . parseDouble ( format) ;
}
4, string 不足几位补0
public static String addZeroForNum ( String str, int strLength) {
int strLen = str. length ( ) ;
if ( strLen < strLength) {
while ( strLen < strLength) {
StringBuffer sb = new StringBuffer ( ) ;
sb. append ( "0" ) . append ( str) ;
str = sb. toString ( ) ;
strLen = str. length ( ) ;
}
}
return str;
}
5, string 字符串数字 转换为百分数
public static String stringChangePercent ( String param) {
double v = stringChangeDouble ( param) * 100 ;
String value = v + "%" ;
return value;
}
6 , 比较double值是否相等
public static Boolean doubleEquality ( double a, double b) {
System . out. println ( Math . abs ( a - b) ) ;
if ( Math . abs ( a - b) < 0.000000002 ) {
return true ;
} else {
return false ;
}
}
7, double 保留十位小数
public static String doubleRoundChangString ( double d) {
BigDecimal decimal = BigDecimal . valueOf ( d) ;
DecimalFormat df = new DecimalFormat ( "0.##########" ) ;
df. setRoundingMode ( RoundingMode . HALF_UP) ;
String format = df. format ( decimal. doubleValue ( ) ) ;
System . out. println ( Double . parseDouble ( format) + "-------返回的double--------" ) ;
double v = Double . parseDouble ( format) ;
String s = String . valueOf ( v) ;
System . out. println ( "s is " + s) ;
return s;
}
8, 不够位数的在前面补0,保留num的长度位数字
public static String autoGenericCode ( String code, int num) {
String result = "" ;
result = String . format ( "%0" + num + "d" , Integer . parseInt ( code) + 1 ) ;
return result;
}
9, 正负数转换
public static Double zFChange ( Double d) {
if ( d != 0d ) {
d = - d;
}
return d;
}
10, String转换为yyyy-MM-dd日期
public static Date strToDate ( String dateStr) {
SimpleDateFormat formatter = new SimpleDateFormat ( "yyyy-MM-dd" ) ;
ParsePosition pos = new ParsePosition ( 0 ) ;
Date date = formatter. parse ( dateStr, pos) ;
return date;
}
11,日期转换为 例如:20210407
public static String getNowDateToString ( Date date) {
SimpleDateFormat sdf = new SimpleDateFormat ( "yyyyMMdd" ) ;
String date2 = sdf. format ( date) ;
System . out. println ( date2) ;
return date2;
}
二,日期转换
1, 获取String类型的纯数字年月日 例如:20210407
public static String getNowDateToString ( ) {
Date date = new Date ( ) ;
SimpleDateFormat sdf = new SimpleDateFormat ( "yyyyMMdd" ) ;
String date2 = sdf. format ( date) ;
System . out. println ( date2) ;
return date2;
}
2, 指定日期加上天数后的日期
public static String plusDay ( int num, Date currdate) throws ParseException {
SimpleDateFormat format = new SimpleDateFormat ( "yyyy-MM-dd" ) ;
System . out. println ( "现在的日期是:" + currdate) ;
Calendar ca = Calendar . getInstance ( ) ;
ca. setTime ( currdate) ;
ca. add ( Calendar . DATE, num) ;
currdate = ca. getTime ( ) ;
String enddate = format. format ( currdate) ;
System . out. println ( "增加天数以后的日期:" + enddate) ;
return enddate;
}
3, 获取指定日期是星期几
public static String getWeekOfDate ( Date date) {
String [ ] weekDays = { "星期日" , "星期一" , "星期二" , "星期三" , "星期四" , "星期五" , "星期六" } ;
Calendar cal = Calendar . getInstance ( ) ;
cal. setTime ( date) ;
int w = cal. get ( Calendar . DAY_OF_WEEK) - 1 ;
System . out. println ( w + "-----index" ) ;
if ( w < 0 ) {
w = 0 ;
}
return weekDays[ w] ;
}
4, 获取指定日期是星期几
public static int getWeekOfDateIndex ( Date date) {
Calendar cal = Calendar . getInstance ( ) ;
cal. setTime ( date) ;
int index = cal. get ( Calendar . DAY_OF_WEEK) - 1 ;
System . out. println ( index + "-----index" ) ;
return index;
}
5, 日期到下一个月的1号
public static String dateGoNextOne ( Date date) {
String endDate = "" ;
SimpleDateFormat format = new SimpleDateFormat ( "yyyy-MM-dd" ) ;
String newDate = format. format ( date) ;
String [ ] dates = newDate. split ( "-" ) ;
int a = Integer . parseInt ( dates[ 1 ] ) ;
int year = Integer . parseInt ( dates[ 0 ] ) ;
if ( a == 12 ) {
a = 1 ;
year++ ;
} else {
a++ ;
}
endDate = year + "-" + a + "-01" ;
return endDate;
}
6, 时间戳转换为yyyy-MM-dd格式
public static String changeTimeToYearMonthDay ( String time) {
SimpleDateFormat sdf = new SimpleDateFormat ( "yyyy-MM-dd" ) ;
String sd = sdf. format ( new Date ( Long . parseLong ( time) ) ) ;
return sd;
}
7, 当前年月 yyyy-MM格式
public static String getCurrentDate2 ( ) {
SimpleDateFormat sdf = new SimpleDateFormat ( "yyyy-MM" ) ;
String sd = sdf. format ( new Date ( ) ) ;
return sd;
}
8, 上一个月 yyyy-MM格式
public static String getLastMonth ( ) {
SimpleDateFormat format = new SimpleDateFormat ( "yyyy-MM" ) ;
Date date = new Date ( ) ;
Calendar calendar = Calendar . getInstance ( ) ;
calendar. setTime ( date) ;
calendar. set ( Calendar . MONTH, calendar. get ( Calendar . MONTH) - 1 ) ;
date = calendar. getTime ( ) ;
String accDate = format. format ( date) ;
return accDate;
}
9, 获取过去时间点
public static long getLastPeroid ( int type, Date date) {
Calendar c = Calendar . getInstance ( ) ;
if ( 0 == type) {
c. setTime ( date) ;
c. add ( Calendar . DATE, - 1 ) ;
Date d = c. getTime ( ) ;
return d. getTime ( ) ;
} else if ( 1 == type) {
c. setTime ( date) ;
c. add ( Calendar . DATE, - 7 ) ;
Date d = c. getTime ( ) ;
return d. getTime ( ) ;
} else if ( 2 == type) {
c. setTime ( date) ;
c. add ( Calendar . MONTH, - 1 ) ;
Date m = c. getTime ( ) ;
return m. getTime ( ) ;
} else if ( 3 == type) {
c. setTime ( date) ;
c. add ( Calendar . YEAR, - 1 ) ;
Date y = c. getTime ( ) ;
return y. getTime ( ) ;
} else if ( 4 == type) {
c. setTime ( date) ;
c. add ( Calendar . YEAR, - 20 ) ;
Date y = c. getTime ( ) ;
return y. getTime ( ) ;
}
c. setTime ( date) ;
c. add ( Calendar . DATE, - 1 ) ;
Date d = c. getTime ( ) ;
return d. getTime ( ) ;
}
10, 本月第一天
public static String getThisMonthStartDay ( ) {
SimpleDateFormat format = new SimpleDateFormat ( "yyyy-MM-dd" ) ;
Calendar thisMonthFirstDateCal = Calendar . getInstance ( ) ;
thisMonthFirstDateCal. set ( Calendar . DAY_OF_MONTH, 1 ) ;
String thisMonthFirstTime = format. format ( thisMonthFirstDateCal. getTime ( ) ) ;
System . out. println ( "本月起始:" + thisMonthFirstTime) ;
return thisMonthFirstTime;
}
11,本月最后一天
public static String getThisMonthEndDay ( ) {
SimpleDateFormat format = new SimpleDateFormat ( "yyyy-MM-dd" ) ;
Calendar thisMonthEndDateCal = Calendar . getInstance ( ) ;
thisMonthEndDateCal. set ( Calendar . DAY_OF_MONTH, thisMonthEndDateCal. getActualMaximum ( Calendar . DAY_OF_MONTH) ) ;
String thisMonthEndTime = format. format ( thisMonthEndDateCal. getTime ( ) ) ;
System . out. println ( "本月末尾:" + thisMonthEndTime) ;
return thisMonthEndTime;
}
12, 上月第一天
public static String getLastMonthStartDay ( ) {
SimpleDateFormat format = new SimpleDateFormat ( "yyyy-MM-dd" ) ;
Calendar lastMonthFirstDateCal = Calendar . getInstance ( ) ;
lastMonthFirstDateCal. add ( Calendar . MONTH, - 1 ) ;
lastMonthFirstDateCal. set ( Calendar . DAY_OF_MONTH, 1 ) ;
String lastMonthFirstTime = format. format ( lastMonthFirstDateCal. getTime ( ) ) ;
System . out. println ( "上月起始:" + lastMonthFirstTime) ;
return lastMonthFirstTime;
}
13, 上月最后一天
public static String getLastMonthEndDay ( ) {
SimpleDateFormat format = new SimpleDateFormat ( "yyyy-MM-dd" ) ;
Calendar lastMonthEndDateCal = Calendar . getInstance ( ) ;
lastMonthEndDateCal. add ( Calendar . MONTH, - 1 ) ;
lastMonthEndDateCal. set ( Calendar . DAY_OF_MONTH, lastMonthEndDateCal. getActualMaximum ( Calendar . DAY_OF_MONTH) ) ;
String lastMonthEndTime = format. format ( lastMonthEndDateCal. getTime ( ) ) ;
System . out. println ( "上月末尾:" + lastMonthEndTime) ;
return lastMonthEndTime;
}