Calendar 时间API

来源:互联网 发布:太空工程师可视化编程 编辑:程序博客网 时间:2024/05/23 00:03
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

0 0
原创粉丝点击