package com.zjjerp.tool;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar; /**
* @Author:Demom
* @Date:2013-8-2,上午9:22:05
*/
public class GetDateTimeTools { /***
* 得到yyyy-MM-dd时间格式
* @param date
* @return
*/
public static String getYYYYMMDD(Date date){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(date);
}
/***
* 得到yyyy-MM-dd hh:mm:ss时间格式
* @param date
* @return
*/
public static String getYYYYMMDDHHMMSS(Date date){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
return sdf.format(date);
}
//获取当月的的10号
public static String getnowMonthtenday(){
Calendar cal = Calendar.getInstance();
int month = cal.get(Calendar.MONTH) + 1;
int year = cal.get(Calendar.YEAR);
StringBuffer sb =new StringBuffer();
sb.append(year);
sb.append("-");
if(month<10){
sb.append("0");
sb.append(month);
}else{
sb.append(month);
}
sb.append("-10");
return sb.toString();
}
//获取当前月的下一个月的10号
public static String getnextMonthtenday(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String strDate = sdf.format(new Date());
int index = strDate.indexOf(" ");
String str1 = strDate.substring(0, index);
String[] str2 = str1.split("-");
String nextDay="";
if(str2[1]!="12"){
int Month = Integer.parseInt(str2[1])+1;
nextDay=str2[0]+"-"+Month+"-"+"10";
}
if(str2[1]=="12" || "12".equals(str2[1])){
int Month = 1;
int Year = Integer.parseInt(str2[0])+1;
nextDay = Year+"-"+Month+"-"+"10";
}
return nextDay;
}
//获取当前年
public static int getCurrentYear(){
Calendar cal = Calendar.getInstance();
int month = cal.get(Calendar.MONTH) + 1;
int year = cal.get(Calendar.YEAR);
return year;
}
//获取当前yue
public static int getCurrentMonth(){
Calendar cal = Calendar.getInstance();
int month = cal.get(Calendar.MONTH) + 1;
return month;
}
/**
* 以上个月为时间轴,获取上个月的时间
* @param status(0 代表获取月的1号,1代表获取月末)
* @return
*/
public static String getLastMonthDay(int status) {
Calendar cal = Calendar.getInstance();
Date date = new Date();
cal.setTime(date);
int year = 0;
int month = cal.get(Calendar.MONTH); // 上个月月份
String newMonth="";
//设置年月
if (month == 0) {
year = cal.get(Calendar.YEAR) - 1;
month = 12;
newMonth=""+month;
} else {
if(month<10){
newMonth="0"+String.valueOf(month);
}else{
newMonth=String.valueOf(month);
}
year = cal.get(Calendar.YEAR);
}
//设置天数
String temp=year + "-" + newMonth.toString();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
Date d = null;
try {
d = format.parse(temp);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
cal.setTime(d); int day =cal.getActualMaximum(Calendar.DAY_OF_MONTH);
if(status==1){
String endDay = year + "-" + newMonth + "-" + day;
return endDay;
}else{
String endDay = year + "-" + newMonth + "-" + "01";
return endDay;
}
}
/**
* 以上个月为时间轴,获取下个月的时间
* @param status(0 代表获取月的1号,1代表获取月末)
* @return
*/
public static String getNextMonthDay(int status) {
Calendar cal = Calendar.getInstance();
int day=cal.get(Calendar.DATE);
SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");
String timestr= sdf.format(new Date());
timestr=timestr.substring(0,8);
if(status==1){
timestr+=String.valueOf(day);
}else{
timestr+="01";
}
return timestr;
}
/***
* 以上个月为时间轴,获取当月的时间
* @param status(0 代表获取月的1号,1代表获取月末)
* @return
*/
public static String getCurrMonthDay(int status) {
Calendar cal = Calendar.getInstance();
Date date = new Date();
cal.setTime(date);
int year = 0;
int month = cal.get(Calendar.MONTH); // 上个月月份
String newMonth="";
//设置年月
if (month == 0) {
year = cal.get(Calendar.YEAR) - 1;
month = 12;
newMonth=""+month;
} else {
if(month<10){
newMonth="0"+String.valueOf(month);
}else{
newMonth=String.valueOf(month);
}
year = cal.get(Calendar.YEAR);
}
//设置天数
String temp=year + "-" + newMonth.toString();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
Date d = null;
try {
d = format.parse(temp);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
cal.setTime(d);
int day =cal.getActualMaximum(Calendar.DAY_OF_MONTH);
if(status==1){
String endDay = year + "-" + newMonth + "-" + day;
return endDay;
}else{
String endDay = year + "-" + newMonth + "-" + "01";
return endDay;
} }
//获取当月5号日前哦
public static String getevery5day(){ Calendar cal = Calendar.getInstance();
int month = cal.get(Calendar.MONTH) + 1;
int year = cal.get(Calendar.YEAR);
StringBuffer sb =new StringBuffer();
sb.append(year);
sb.append("-");
if(month<=10){
sb.append("0");
sb.append(month);
}else{
sb.append(month);
}
sb.append("-05");
return sb.toString();
} //合同续约
public static String datexuyue(int year){ SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");
String now = sdf.format(new Date());
String[] nowStr = now.split("-");
int year1 = Integer.parseInt(nowStr[0])+year;
StringBuffer sb = new StringBuffer();
sb.append(year1+"-"+nowStr[1]+"-"+nowStr[2]);
return sb.toString();
}
//得到N天后的日期
public static String getBeforeAfterDate(String datestr, int day) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
java.sql.Date olddate = null;
try {
df.setLenient(false);
olddate = new java.sql.Date(df.parse(datestr).getTime());
} catch (ParseException e) {
throw new RuntimeException("日期转换错误");
}
Calendar cal = new GregorianCalendar();
cal.setTime(olddate); int Year = cal.get(Calendar.YEAR);
int Month = cal.get(Calendar.MONTH);
int Day = cal.get(Calendar.DAY_OF_MONTH); int NewDay = Day + day; cal.set(Calendar.YEAR, Year);
cal.set(Calendar.MONTH, Month);
cal.set(Calendar.DAY_OF_MONTH, NewDay); return new java.sql.Date(cal.getTimeInMillis()).toString();
} //根据两个日期A、B计算AB之间的天数
public static long getQuot(String time1, String time2){
long quot = 0;
SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
try { Date date1 = ft.parse( time1 );
Date date2 = ft.parse( time2 );
quot = date1.getTime() - date2.getTime();
quot = quot / 1000 / 60 / 60 / 24;
} catch (ParseException e) {
e.printStackTrace();
}
return quot;
}
//得到N月后的日期
public static String GetSysDate(String format, String StrDate, int year, int month,
int day) {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sFmt = new SimpleDateFormat(format);
cal.setTime(sFmt.parse( (StrDate), new ParsePosition(0))); if (day != 0) {
cal.add(cal.DATE, day);
}
if (month != 0) {
cal.add(cal.MONTH, month);
}
if (year != 0) {
cal.add(cal.YEAR, year); }
return sFmt.format(cal.getTime());
} public static void main(String[] args) {
System.out.println(GetSysDate("yyyy-MM-dd", "2004-01-18", 0, 13, 0));
}
}

java 常用时间操作类,计算到期提醒,N年后,N月后的日期的更多相关文章

  1. JAVA常用时间操作类

    http://www.360doc.com/content/10/1210/17/2703996_76839640.shtml    在程序里面要获取当前的开始时间和结束时间,以及当前天所在的周的开始 ...

  2. Java常用API(ArrayList类)

    Java常用API(ArrayList类) 我们为什么要使用ArrayList类? 为了更加方便的储存对象,因为使用普通的数组来存储对象太过麻烦了,因为数组的一个很大的弱点就是长度从一开始就固定了,所 ...

  3. Python常用时间操作总结【取得当前时间、时间函数、应用等】转载

    Python常用时间操作总结[取得当前时间.时间函数.应用等] 转载  2017-05-11   作者:清风乐逍遥    我要评论 这篇文章主要介绍了Python常用时间操作,包括取得当前时间.时间函 ...

  4. Java常用API(Arrays类)

    Java常用API(Arrays类) 什么是Arrays类? java.util.Arrays 此类包含用来操作数组的各种方法,比如排序和搜索等.其所有方法均为静态方法,调用起来 非常简单. 这里我们 ...

  5. Java常用API(Scanner类)

    Java常用API( Scanner类)1 1.Scanner类 首先给大家介绍一下什么是JavaAPI API(Application Programming Interface),应用程序编程接口 ...

  6. Java常用API(Math类)

    Java常用API(Math类) Math类的作用 java.lang.Math 类包含用于执行基本数学运算的方法,如初等指数.对数.平方根和三角函数.类似这样的工具 类,其所有方法均为静态方法,并且 ...

  7. Java常用API(String类)

    Java常用API(String类) 概述: java.lang.String 类代表字符串.Java程序中所有的字符串文字(例如 "abc" )都可以被看作是实现此类的实例 1. ...

  8. Java常用API(Random类)

    Java常用API(Random类) Random:是一个用于生成随机数的类 构造方法 public Random() :创建一个新的随机数生成器. 返回随机数的方法 public int nextI ...

  9. java/javascript 时间操作工具类

    一.java 时间操作工具类 import org.springframework.util.StringUtils; import java.text.ParseException; import ...

随机推荐

  1. 【鸿蒙开发板试用报告】用OLED板实现FlappyBird小游戏(中)

    小伙伴们久等了,在上一篇<[开发板试用报告]用OLED板实现FlappyBird小游戏(上)>中,我们本着拿来主义的原则,成功的让小鸟在OLED屏幕上自由飞翔起来,下面我们将加入按钮交互功 ...

  2. Linux下的MediaWiki的部署启动遇到的问题与解决方案

    1. MySQL安装不成功 解决方案:https://bbs.csdn.net/topics/394377536 2. no space left on device ubuntu 解决方案:http ...

  3. jvm系列(二)jvm垃圾收集器与内存分配策略

    众所周知,在java语言中,内存分配和回收是由jvm自动管理的.因此内存的分配和回收也是jvm三大功能之一.垃圾收集器(GC)需要完成三件事情: 哪些内存需要回收? 什么时候进行回收? 如何回收? 本 ...

  4. PTA天梯赛校内模拟

    最长对称子串 || 区间dp || 马拉车 dp[i][j]表示区间[i, j]是否为回文串,若是则为1,不是则为0. 边界条件: 1. 区间长度为1,dp为1.(奇数个字符递推的起始情况) 2. 区 ...

  5. VS Code C++ 项目快速配置模板

    两个月前我写过一篇博客 Windows VS Code 配置 C/C++ 开发环境 ,主要介绍了在 VS Code 里跑简单 C 程序的一些方法.不过那篇文章里介绍的方法仅适用于单文件程序,所以稍微大 ...

  6. python语法元素的名称

    变量 什么是变量? """ 变量是保存和表示数据值的一种语法元素,在程序中十分常见.顾名思义,变量的值是可以改变的,能够通过赋值(使用等号"=")方式 ...

  7. java顺序、选择、循环结构

    一.顺序结构 二.选择结构 1.if都执行 2.if else if else 条件满足才执行 3.选择结构switch 一个case后有多条语句要加花括号 多个case的值不能相同 case中要加b ...

  8. 【Usaco 2009 Silver】JZOJ2020年9月19日提高B组T1 音乐节拍

    [Usaco 2009 Silver]JZOJ2020年9月19日提高B组T1 音乐节拍 题目 Description FJ准备教他的奶牛弹奏一首歌曲,歌曲由N(1<=N<=50,000) ...

  9. Java 安全之Java Agent

    Java 安全之Java Agent 0x00 前言 在前面发现很多技术都会去采用Java Agent该技术去做实现,比分说RASP和内存马(其中一种方式).包括IDEA的这些破解都是基于Java A ...

  10. Leetcode学习笔记(5)

    之前断了一段时间没做Leetcode,深感愧疚,重新续上 题目1 ID104 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点 ...