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的更多相关文章

  1. DateUtils 学习记录1

    开发过程中很多时候都需要处理各种各样的日期..有些项目可能还会有自己的DateUtil.... 其实apache commons lang3有一个很好用的日期处理工具类,叫DateUtils... 基 ...

  2. System.DateUtils 4. IsValidDateTime... 有效时间判断

    编译版本:Delphi XE7 function IsValidDate(const AYear, AMonth, ADay: Word): Boolean;function IsValidTime( ...

  3. 【转】日期工具类DateUtils

    转自csdn,原作者:jzhf2012 package date.util; import java.text.ParseException; import java.text.SimpleDateF ...

  4. 【转】apache DateFormatUtils 与 DateUtils 的使用

    在Apache Commons项目的Lang里面,有两个类:DateUtils和DateFormatUtils,专门用于处理时间日期转换.它们在 org.apache.commons.lang.tim ...

  5. Delphi DateUtils时间单元

    Uses DateUtils //时间单元,非常有用. 记得引用这个单元,不然不能用. CompareDate 比较两个日期时间值日期部分的大小 CompareDateTime 比较两个日期时间值的大 ...

  6. 安卓开发:DateUtils

    public class DateUtils{ /** * 显示友好时间 * * @param ms * @return */ public static String getTimeDes(long ...

  7. 时间处理工具类DateUtils

    public class DateUtils {         public static final String                            SHORT_DATE    ...

  8. 使用DateUtils和DateFormatUtils处理时间日期转换与SimpleDateFormat的区别

    在Apache Commons项目的Lang里面,有两个类:DateUtils和DateFormatUtils,专门用于处理时间日期转换.它们在 org.apache.commons.lang.tim ...

  9. java日期的运用(DateUtils工具类)

    public static void main(String[] args) { Date now = new Date(); SimpleDateFormat sd = new SimpleDate ...

随机推荐

  1. POJ 1226 后缀数组

    题目链接:http://poj.org/problem?id=1226 题意:给定n个字符串[只含大小写字母],求一个字符串要求在n个串或者他们翻转后的串的出现过.输出满足要求的字符串的长度 思路:根 ...

  2. psql-05数据库,模式

    数据的组织结构 数据库:表,索引:数据行 PostgreSQL中一个服务(实例)可以有多个数据库:而一个数据库不能属于多个实例; 数据库 创建数据库 create database name [own ...

  3. 用Python做自然语言处理必知的八个工具【转载】

    Python以其清晰简洁的语法.易用和可扩展性以及丰富庞大的库深受广大开发者喜爱.其内置的非常强大的机器学习代码库和数学库,使Python理所当然成为自然语言处理的开发利器. 那么使用Python进行 ...

  4. iOS10 UI设计基础教程

    iOS10 UI设计基础教程 介绍:本教程针对iOS初级开发人员,基于iOS 10系统,使用Swift 3.0语言讲解如何进行UI设计.本教程内容涵盖UI基础构成.UI元素.自动布局.自适应UI.UI ...

  5. wordpress发送测试邮件

    下面的邮箱设置使用了qq邮箱的设置 写上接收测试邮件的邮箱  再send test

  6. 由case 和 break 引发的思考

    我在使用case 语句的时候,发现结果不对劲,原来我是忽略了break这个语句的添加 然后,我要反思的是,这样基本的语句,我这样的错误也能犯,证明我自己之前还没有真正掌握这个语句的使用. 所以,真正地 ...

  7. The name does not exist in the namespace error in XAML

    方法1:In the solution property page, check the platform of the assembly that contains "UpdatingMe ...

  8. POJ2472106 miles to Chicago

    106 miles to Chicago Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 3931   Accepted: 1 ...

  9. BZOJ2164 : 采矿

    树链剖分+线段树,每个节点维护以下信息: (1)单独在某个点分配$i$个人的最大收益.可以$O(m)$合并. (2)分配$i$个人的最大收益.可以用$O(m^2)$合并. 时间复杂度$O(c(m^2\ ...

  10. Windows下查看端口冲突的进程

    在tomcat部署中,经常遇到80端口被占用,下面总结了两条查看端口进程的方法. 查看端口方法: netstat -aon|findstr "80"   如图,使用80端口的进程列 ...