时间工具类方法-Java实现
来源:互联网 发布:漫威电影观看顺序知乎 编辑:程序博客网 时间:2024/06/05 20:38
实际工作中经常遇到需要处理如下时间相关的场景需要,所以写个工具类方法,方便使用.
1>获取指定时间的前一天,后一天
2>获取指定时间的前一周
3>获取指定时间的前一月
4>获取指定时间的一天的起止时间
这个工具类方法可以写个主方法进行测试,当然也可以进一步完善修改.代码如下:
=========================================================================
package com.tgy.util;
import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* DateUtil
* @author Jerry Tong
* @date 2017-10-24
*
*/
public class DateUtil {
private static final String TIME_PATTERN0="yyyy-MM-dd";
private static final String TIME_PATTERN1="yyyy-MM-dd HH:mm:ss";
private static SimpleDateFormat sdf;
/**
* @param cal A specified instance of the Calendar.
* @return Get the A day's startTime adn endTime.<br>
* <ul>
* <li>eg:Current time is 2017-10-24 15:08:00</li>
* <li>return list will be:list.get(0)=2017-10-24 00:00:00,list.get(1)=2017-10-24 23:59:59</li>
* </ul>
* @throws Exception
*/
public static List<Date> getDate(Calendar cal) throws Exception{
if(cal==null)
throw new Exception("Null exception:cal is null!");
Date curDate=cal.getTime();
sdf=new SimpleDateFormat(TIME_PATTERN0);
String strCurDate=sdf.format(curDate);
sdf=new SimpleDateFormat(TIME_PATTERN1);
List<Date>list=new ArrayList<Date>();
list.add(sdf.parse(strCurDate+" 00:00:00"));
list.add(sdf.parse(strCurDate+" 23:59:59"));
return list;
}
/**
*
* @param cal A specified instance of the Calendar
* @return Get the specified cal's previous Date and next Date.<br>
* <ul>
* <li>eg:</li>
* <li>Current time is 2017-10-24 15:08:00</li>
* <li>return list will be:list.get(0)="2017-10-23",list.get(1)="2017-10-25"</li>
* </ul>
* @throws Exception
*/
public static List<String> getPreNextDate(Calendar cal) throws Exception{
if(cal==null)
throw new Exception("Null exception:cal is null!");
List<String>list=new ArrayList<String>();
cal.add(Calendar.DATE, -1);
list.add(sdf.format(cal.getTime()));
cal.add(Calendar.DATE, 2);
list.add(sdf.format(cal.getTime()));
return list;
}
/**
*
* @param cal A specified instance of the Calendar
* @return Get a list of last week(from Monday to Sunday)<br>
* <ul>
* <li>eg:</li>
* <li>Current time is 2017-10-24 15:08:00</li>
* <li>Return list will be:list.get(0)="2017-10-16",list.get(1)="2017-10-22"</li>
* </ul>
* @throws Exception
*/
public static List<String> getPreWeekDate(Calendar cal) throws Exception{
if(cal==null)
throw new Exception("Null exception:cal is null!");
List<String>list=new ArrayList<String>();
sdf=new SimpleDateFormat(TIME_PATTERN0);
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
cal.add(Calendar.DATE, -1*7);
list.add(sdf.format(cal.getTime()));
cal.add(Calendar.DATE, 6);
list.add(sdf.format(cal.getTime()));
return list;
}
/**
*
* @param cal A specified instance of the Calendar
* @return Get a List of last month(from 1st day to the last day)<br>
* eg:Current time is 2017-10-24 15:08:00,call this method<br>
* you will get the list(list.get(0)="2017-09-01",list.get(1)="2017-09-30")
* @throws Exception
*/
public static List<String> getPreMonthDate(Calendar cal) throws Exception{
if(cal==null)
throw new Exception("Null exception:cal is null!");
sdf=new SimpleDateFormat(TIME_PATTERN0);
List<String>list=new ArrayList<String>();
sdf=new SimpleDateFormat(TIME_PATTERN0);
cal.add(Calendar.MONTH, -1);
cal.set(Calendar.DAY_OF_MONTH, 1);
list.add(sdf.format(cal.getTime()));
cal.add(Calendar.MONTH, 1);
cal.add(Calendar.DATE, -1);
list.add(sdf.format(cal.getTime()));
return list;
}
}
- 时间工具类方法-Java实现
- 时间工具类,一些常见的时间实现方法
- java时间工具类
- java 时间工具类
- Java时间工具类
- Java时间工具类
- 时间工具类--java
- Java时间工具类
- java 时间工具类
- java 时间类 工具
- Java时间工具类
- 时间工具类java
- Java时间工具类
- JAVA 时间工具类
- #.java 时间工具类
- java 时间工具类
- [Java]时间工具类
- Java获取各种常用时间方法(工具类)
- Windows Android Studio安装教程
- 网页上不可选中文字的那些事儿
- 移动端样式小技巧
- 结对编程的第1例:主要着眼于,解决RecyclerView的头部伸缩时,抖动的功能
- android scrollView 内部子布局MATCH_PARENT却无法填充满屏幕问题
- 时间工具类方法-Java实现
- MVP
- Java在web页面上中文URL乱码解决
- 线程中CreateEvent和SetEvent及WaitForSingleObject
- Sequence discriminative training
- mac 终端 常用命令
- Rehashing
- 牛顿插值函数C语言程序实现
- 上下拉电阻的作用