Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

获取当前时间若干年、月、日、时等工具时间工具类

$
0
0

package com.hpli.demo;

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

 

public class TimeUtls {

 public static int PLUS = 1;

 public static int MINUS = -1;

 public final static int YEAR = GregorianCalendar.YEAR;

 public final static int MONTH = GregorianCalendar.MONTH;

 public final static int DAY_OF_MONTH = GregorianCalendar.DAY_OF_MONTH;

 public final static int HOUR_OF_DAY = GregorianCalendar.HOUR_OF_DAY;

 public final static int MINUTE = GregorianCalendar.MINUTE;

 public final static int SECOND = GregorianCalendar.SECOND;

 /**
  * 将时间增加若干年
  */
 public static Date getTimePlusYear(Date time, int year) {
  return getTimeOperation(time, PLUS, YEAR, year);
 }

 /**
  * 将时间减少若干年
  */
 public static Date getTimeMinusYear(Date time, int year) {
  return getTimeOperation(time, PLUS, MINUS, year);
 }

 /**
  * 将时间增加若干月
  */
 public static Date getTimePlusMonth(Date time, int month) {
  return getTimeOperation(time, PLUS, MONTH, month);
 }

 /**
  * 将时间减少若干月
  */
 public static Date getTimeMinusMonth(Date time, int month) {
  return getTimeOperation(time, PLUS, MONTH, month);
 }

 /**
  * 将时间增加若干日
  */
 public static Date getTimePlusDay(Date time, int day) {
  return getTimeOperation(time, PLUS, DAY_OF_MONTH, day);
 }

 /**
  * 将时间减少若干日
  */
 public static Date getTimeMinusDay(Date time, int day) {
  return getTimeOperation(time, PLUS, DAY_OF_MONTH, day);
 }

 /**
  * 将时间增加若干时
  */
 public static Date getTimePlusHour(Date time, int hour) {
  return getTimeOperation(time, PLUS, HOUR_OF_DAY, hour);
 }

 /**
  * 将时间减少若干时
  */
 public static Date getTimeMinusHour(Date time, int hour) {
  return getTimeOperation(time, PLUS, HOUR_OF_DAY, hour);
 }

 /**
  * 将时间增加若干分
  */
 public static Date getTimePlusMinute(Date time, int minute) {
  return getTimeOperation(time, PLUS, MINUTE, minute);
 }

 /**
  * 将时间减少若干分
  */
 public static Date getTimeMinusMinute(Date time, int minute) {
  return getTimeOperation(time, PLUS, MINUTE, minute);
 }

 /**
  * 将时间增加若干秒
  */
 public static Date getTimePlusSecond(Date time, int second) {
  return getTimeOperation(time, PLUS, SECOND, second);
 }

 /**
  * 将时间减少若干分
  */
 public static Date getTimeMinusSecond(Date time, int second) {
  return getTimeOperation(time, PLUS, SECOND, second);
 }

 /**
  * 当前时间
  *
  * @return
  */
 public static Date getCurrentTime() {
  GregorianCalendar calendar = new GregorianCalendar();
  Date date = calendar.getTime();
  return date;
 }

 /**
  * 时间核心运算函数
  *
  * @param time
  *            原始时间
  * @param operationType
  *            操作类型,增加或减少
  * @param field
  *            时间域:年、月、日、时、分、秒
  * @param value
  *            增加值
  * @return 运算后的时间
  */
 public static Date getTimeOperation(Date time, int operationType,
   int field, int value) {
  GregorianCalendar calendar = new GregorianCalendar();
  calendar.setTime(time);
  int newValue = 0;

  if (operationType == PLUS) {
   newValue = calendar.get(field) + value;
  } else if (operationType == MINUS) {
   newValue = calendar.get(field) - value;
  }
  calendar.set(field, newValue);

  return calendar.getTime();
 }

 public static void main(String[] args) {
  SimpleDateFormat dateFormat = new SimpleDateFormat(
  "yyyy-MM-dd HH:mm:ss.SSSSSSZ");
  
  Date year = getTimePlusYear(getCurrentTime(), 3);
  System.err.println("year: "+dateFormat.format(year));
  
  Date month = getTimePlusMonth(getCurrentTime(), 3);
  System.err.println("month: "+dateFormat.format(month));
  
  Date Day = getTimePlusDay(getCurrentTime(), 3);
  System.err.println("Day: "+dateFormat.format(Day));
  
  Date Hour = getTimePlusHour(getCurrentTime(), 3);
  System.err.println("Hour: "+dateFormat.format(Hour));
  
  Date Minute = getTimePlusMinute(getCurrentTime(), 3);
  System.err.println("Minute: "+dateFormat.format(Minute));
  
  Date Second = getTimePlusSecond(getCurrentTime(), 3);
  System.err.println("Second: "+dateFormat.format(Second));
 }
}

作者:hpli148 发表于2013-3-9 17:22:58 原文链接
阅读:40 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>