Calendar 时间API

package com.hz.test;

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

public class CalendarTest {

    public static void main(String[] args) {

        Calendar ca = Calendar.getInstance();
        //获取当前日期
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
        String time = sdf.format(ca.getTime());
        System.out.println(time);
        //进位添加
        ca.add(Calendar.MINUTE, 10);
        sdf = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
        time = sdf.format(ca.getTime());
        System.out.println(time);
        //不进位添加
        ca.roll(Calendar.MINUTE, 56);
        sdf = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
        time = sdf.format(ca.getTime());
        System.out.println(time);

    }
}

输出结果:

12-13-2016 11:15:36
12-13-2016 11:25:36
12-13-2016 11:21:36

public class DateFormatUtil {
    private String currentTime;

    //获得当前时间
    public String getCurrentTime() {

        Date date = new Date();
        DateFormat mediumTime = DateFormat.getDateTimeInstance(
                DateFormat.MEDIUM, DateFormat.MEDIUM);
//      DateFormat fullTime = DateFormat.getDateTimeInstance(
//              DateFormat.FULL, DateFormat.FULL);
        //DateFormat.SHORT
        //DateFormat.LONG
        currentTime = mediumTime.format(date);
        return currentTime;
    }
}

输出结果:

2016-12-13 11:15:17

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值