Java日期处理

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by JingYuanhang on 2017/3/21.
 */
public class Test {

    public static void main(String[] args) {
        //格式化日期方法
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyy-MM-dd HH:mm:ss");
        Date date = new Date();
        //取当前日期的前一天
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, -1);
        Date bDate = calendar.getTime();
        System.out.println(simpleDateFormat.format(date));
        System.out.println(simpleDateFormat.format(bDate));
        //求时间差几分钟之类的
        long diff = date.getTime() - bDate.getTime();
        System.out.println(diff / (1000 * 60));
    }
}

你可能感兴趣的:(java,string)