DateUtils
package com.vcredit.ddcash.batch.util;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* 日期处理工具类
* Created by xutao on 2016/11/3 0003.
*/
public class DateUtils {
/**
* 获取年
*
* @param date 参数
* @return 年
*/
public static String getYear(Date date) {
if (date == null) {
throw new RuntimeException("参数date不能为空!");
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
return sdf.format(date);
}
/**
* 获取月
*
* @param date 参数
* @return 月
*/
public static String getMonth(Date date) {
if (date == null) {
throw new RuntimeException("参数date不能为空!");
}
SimpleDateFormat sdf = new SimpleDateFormat("MM");
return sdf.format(date);
}
/**
* 获取日
*
* @param date 参数
* @return 日
*/
public static String getDay(Date date) {
if (date == null) {
throw new RuntimeException("参数date不能为空!");
}
SimpleDateFormat sdf = new SimpleDateFormat("dd");
return sdf.format(date);
}
/**
* 按照指定的格式转换日期
*
* @param sourceDate 日期
* @param pattern 格式
* @return 日期字符串
*/
public static String dateToString(Date sourceDate, String pattern) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.format(sourceDate);
}
/**
* 按照默认的格式转换日期
*
* @param sourceDate 日期
* @return 日期字符串
*/
public static String dateToString(Date sourceDate) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(sourceDate);
}
/**
* 毫秒值转换成日期字符串
*
* @param milliseconds 毫秒值
* @return 日期字符串
*/
public static String millisToString(Long milliseconds) {
if (milliseconds == null) {
throw new RuntimeException("时间(毫秒)不能为空");
}
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(milliseconds);
return dateToString(calendar.getTime());
}
/**
* 按照指定格式转换毫秒值到日期字符串
*
* @param milliseconds 毫秒值
* @param pattern 格式
* @return 日期字符串
*/
public static String millisToString(Long milliseconds, String pattern) {
if (milliseconds == null) {
throw new RuntimeException("时间(毫秒)不能为空");
}
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(milliseconds);
return dateToString(calendar.getTime(), pattern);
}
}
DateUtils的更多相关文章
- DateUtils 学习记录1
开发过程中很多时候都需要处理各种各样的日期..有些项目可能还会有自己的DateUtil.... 其实apache commons lang3有一个很好用的日期处理工具类,叫DateUtils... 基 ...
- System.DateUtils 4. IsValidDateTime... 有效时间判断
编译版本:Delphi XE7 function IsValidDate(const AYear, AMonth, ADay: Word): Boolean;function IsValidTime( ...
- 【转】日期工具类DateUtils
转自csdn,原作者:jzhf2012 package date.util; import java.text.ParseException; import java.text.SimpleDateF ...
- 【转】apache DateFormatUtils 与 DateUtils 的使用
在Apache Commons项目的Lang里面,有两个类:DateUtils和DateFormatUtils,专门用于处理时间日期转换.它们在 org.apache.commons.lang.tim ...
- Delphi DateUtils时间单元
Uses DateUtils //时间单元,非常有用. 记得引用这个单元,不然不能用. CompareDate 比较两个日期时间值日期部分的大小 CompareDateTime 比较两个日期时间值的大 ...
- 安卓开发:DateUtils
public class DateUtils{ /** * 显示友好时间 * * @param ms * @return */ public static String getTimeDes(long ...
- 时间处理工具类DateUtils
public class DateUtils { public static final String SHORT_DATE ...
- 使用DateUtils和DateFormatUtils处理时间日期转换与SimpleDateFormat的区别
在Apache Commons项目的Lang里面,有两个类:DateUtils和DateFormatUtils,专门用于处理时间日期转换.它们在 org.apache.commons.lang.tim ...
- java日期的运用(DateUtils工具类)
public static void main(String[] args) { Date now = new Date(); SimpleDateFormat sd = new SimpleDate ...
随机推荐
- clone github的代码
终端执行:git clone 连接.git #不用sudo
- no-jquery 03 Ajax
Ajax Requests GETting var xhr = new XMLHttpRequest(); xhr.open('GET', encodeURI('myservice/username? ...
- js-Ajax与Comet
Ajax与Comet: 1.Ajax技术的核心是XHR(XMLHTTPRequest对象) 创建xhr对象: function createXHR(){ if(typeof XMLHttpReques ...
- js从字符串中提取身份证号,连续18位数字
<!DOCTYPE html> <html> <head> <title>提取身份证号</title> <meta charset=& ...
- JS 中面向对象的5种写法
//第1种写法 function Circle(r) { this.r = r; } Circle.PI = 3.14159; Circle.prototype.area = function() { ...
- 运用datalist标签实现用户的搜索列表
datalist是一个很强大的HTML5标签,支持一般类似于模糊查询,以前都是需要js来做的.下面是一个datalist配合js的小例子,主要是实现用户是否存在,以及添加过程中是否重复的判断. 首先是 ...
- http://www.cnblogs.com/meiCode/p/5896239.html
http://www.cnblogs.com/meiCode/p/5896239.html
- Codeforces Round #341 (Div. 2)
在家都变的懒惰了,好久没写题解了,补补CF 模拟 A - Wet Shark and Odd and Even #include <bits/stdc++.h> typedef long ...
- 疯狂java学习笔记之面向对象(三) - 方法所属性和值传递
方法的所属性: 从语法的角度来看:方法必须定义在类中 方法要么属于类本身(static修饰),要么属于实例 -- 到底是属于类还是属于对象? 有无static修饰 调用方法时:必须有主调对象(主语,调 ...
- Nginx 利用 X-Accel-Redirect response.setHeader 控制文件下载
nginx.conf location / { proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP ...