data以及calendar,一点点累加

Posted notchangeworld

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了data以及calendar,一点点累加相关的知识,希望对你有一定的参考价值。

package com.cloudmap.test;

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

public class testCalender2 {
    public static void main(String[] args) {

        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH)+1;
        int day = calendar.get(Calendar.DATE);
        int hour = calendar.get(Calendar.HOUR);
        int minute = calendar.get(Calendar.MINUTE);
        int second = calendar.get(Calendar.SECOND);

        System.out.println(year);
        System.out.println(month);
        System.out.println(day);
        System.out.println(hour);
        System.out.println(minute);
        System.out.println(second);


        //获取当前时间的起始周日期以及结束周日期,以及前几周或后几周
        int i = 0;
        //printWeekStartAndEnd(i);
        //获取当前日期本年的第多少周
        //printWeekOfYear();
    }

    private static void printWeekOfYear() {
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.setTime(new Date());

        System.out.println(calendar.get(Calendar.WEEK_OF_YEAR));
    }


    private static void printWeekStartAndEnd(int i) {
        Calendar calendarRange = Calendar.getInstance();
         calendarRange.setTime(new Date());
        calendarRange.add(Calendar.DAY_OF_YEAR,i);
        int currentDay = calendarRange.get(Calendar.DAY_OF_WEEK);
        if (currentDay == 1) {
            currentDay = 7;
        } else {
            currentDay = currentDay - 1;
        }
        calendarRange.add(Calendar.DAY_OF_YEAR, 1 - currentDay);
        Date dateRange = calendarRange.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String weekStart = format.format(dateRange);
        System.out.println(weekStart);
        calendarRange.setTime(dateRange);
        calendarRange.add(Calendar.DAY_OF_YEAR, 6);
        String weekEnd = format.format(calendarRange.getTime());
        System.out.println(weekEnd);
    }
}

 

以上是关于data以及calendar,一点点累加的主要内容,如果未能解决你的问题,请参考以下文章

java 怎样让一个日期累加,2010-12-16 累加到2010-12-30

Java Calendar 类的时间操作

Java Calendar 类的时间操作

Calendar 日历类的时间操作

Java Calendar 类的时间操作.RP

Java Calendar 类的时间操作