首页 > 代码库 > 时间迭代和BigDecimal操作

时间迭代和BigDecimal操作

常规小操作的代码:

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;

public class TestCommon {
    private static SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
    public static void main(String[] args) {    
        System.out.println("===时间操作===");
        long times= 1471253315716l ;
        Timestamp ts =new Timestamp(times);
        System.out.println(df.format(ts));
        
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH)-30);
        System.out.println("---"+df.format(calendar.getTime()));
        
        System.out.println("===运算操作===");
        BigDecimal big1 = new BigDecimal(654);
        BigDecimal big2 = new BigDecimal("354.254788");
        //big1.compareTo(big2) = 0 
        System.out.println((big1.add(big2)).setScale(2,BigDecimal.ROUND_HALF_UP));        
        System.out.println(big1.divide(new BigDecimal(2), 2,BigDecimal.ROUND_HALF_UP));
        
        System.out.println("===迭代操作===");
        Iterable<BigDecimal> values= new ArrayList<>();
        BigDecimal sum = new BigDecimal(0);
        for (Iterator iter = values.iterator(); iter.hasNext();) {
            BigDecimal val = (BigDecimal)iter.next();
            sum = sum.add(val);
        }
    }
}

 

时间迭代和BigDecimal操作