小时数转成时分秒格式
public static String getDateByHour ( double manHour) {
BigDecimal manHourBig = new BigDecimal ( manHour) ;
BigDecimal bigDecimal = new BigDecimal ( 3600 ) ;
BigDecimal bigDecimalM = new BigDecimal ( 60 ) ;
BigDecimal multiply = manHourBig. multiply ( bigDecimal) ;
int hh = multiply. divide ( bigDecimal, 0 , BigDecimal. ROUND_DOWN) . intValue ( ) ;
BigDecimal[ ] results = multiply. divideAndRemainder ( bigDecimal) ;
BigDecimal result = results[ 1 ] ;
int mm = result. divide ( bigDecimalM, 0 , BigDecimal. ROUND_DOWN) . intValue ( ) ;
BigDecimal[ ] bigDecimals = result. divideAndRemainder ( bigDecimal) ;
int ss = bigDecimals[ 0 ] . intValue ( ) ;
return hh+ ":" + mm+ ":" + ss;
}
简单调用
public static void main ( String[ ] args) {
String dateByHour = getDateByHour ( 2.5 ) ;
System. out. println ( dateByHour) ;
}
输出结果
2 : 30 : 0
两个时间转成小时数
public static String getDatePoor ( Date nowDate, Date endDate) {
long nd = 1000 * 24 * 60 * 60 ;
long nh = 1000 * 60 * 60 ;
long nm = 1000 * 60 ;
long diff = endDate. getTime ( ) - nowDate. getTime ( ) ;
Integer min = Integer. valueOf ( ( int ) ( diff / nm) ) ;
if ( min <= 0 ) {
return new BigDecimal ( 0 ) . toString ( ) ;
}
BigDecimal bigDecimal = new BigDecimal ( min) ;
BigDecimal b = new BigDecimal ( 60 ) ;
BigDecimal divide = bigDecimal. divide ( b, 2 , BigDecimal. ROUND_HALF_UP) ;
return divide. toString ( ) ;
}
简单调用
public static void main ( String[ ] args) throws ParseException {
SimpleDateFormat dateFormat1 = new SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss" ) ;
String datePoor1 = getDatePoor (
dateFormat1. parse ( "2021-10-10 12:00:00" ) ,
dateFormat1. parse ( "2021-10-10 14:30:00" ) ) ;
System. out. println ( datePoor1+ "h" ) ;
SimpleDateFormat dateFormat2 = new SimpleDateFormat ( "HH:mm:ss" ) ;
String datePoor2 = getDatePoor (
dateFormat2. parse ( "12:00:00" ) ,
dateFormat2. parse ( "14:30:00" ) ) ;
System. out. println ( datePoor2+ "h" ) ;
}
输出结果
2.50 h
2.50 h