SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

 1.Calendar 转化 String 

//获取当前时间的具体情况,如年,月,日,week,date,分,秒等 
Calendar calendat =
Calendar.getInstance();

SimpleDateFormat
sdf = new SimpleDateFormat("yyyy-MM-dd");

String dateStr = sdf.format(calendar.getTime());

2.String 转化Calendar

String
str="2010-5-27";
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");

Date
date =sdf.parse(str);

Calendar
calendar = Calendar.getInstance();

calendar.setTime(date);

3.Date
转化String

SimpleDateFormat
sdf= new SimpleDateFormat("yyyy-MM-dd");

String
dateStr=sdf.format(new Date());

4.String
转化Date
String str="2010-5-27";

SimpleDateFormat
sdf= new SimpleDateFormat("yyyy-MM-dd");

Date
birthday = sdf.parse(str);

5.Date
转化Calendar

Calendar
calendar = Calendar.getInstance();
calendar.setTime(new java.util.Date());

6.Calendar转化Date

Calendar
calendar = Calendar.getInstance();
java.util.Date date =calendar.getTime();

7.Date
转成 String

System.out.println(sdf.format(new
Date()));

8.String
转成
Timestamp

Timestamp
ts = Timestamp.valueOf("2011-1-14 08:11:00");

9.Timestamp
转成 String

sdf.format(ts);

TimestampDate多数用法是一样的。

10.Date

TimeStamp

SimpleDateFormat
df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

String
time = df.format(new Date());

Timestamp
ts = Timestamp.valueOf(time);

11.日期比较大小

String
ti="2010-11-25 20:11:00";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd
HH:mm:ss"); 
Date time=sdf.parse(ti);

String
ti2="2011-11-26 20:11:00";
Date time2=sdf.parse(ti2);

int
c=ti2.compareTo(ti);
if(c>0){
    System.out.println(ti+"大");
}else if(c==0){

System.out.println("一样大");

}else{
    System.out.println(ti2+"大");
}

12.Date/
Timestamp
转成
Calendar 

Calendar
calendar = Calendar.getInstance();
calendar.setTime(startDate);

calendar.add(Calendar.YEAR,
2);   //日期加2年
System.out.println(calendar.getTime());
calendar.add(Calendar.DATE, -30);     //日期加30天
System.out.println(calendar.getTime());
calendar.add(Calendar.MONTH, 3);  //日期加3个月
System.out.println(calendar.getTime());

13.将毫秒数转为String

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Timestamp scurrtest = new Timestamp(millis);
String time=sdf.format(scurrtest);

public static String
convert(Timestamp time) {

SimpleDateFormat df = new
SimpleDateFormat("yyyy-MM-dd
HH:mm:ss");

String result =
df.format((Date) time);

return
result;

}

public static
Timestamp getNow() {

Date date = new
Date();

SimpleDateFormat simDate = new
SimpleDateFormat("yyyy-MM-dd
HH:mm:ss");

Timestamp now=Timestamp.valueOf(simDate.format(date));

return now;

}

public static String
dateConvertToString(Date date){

SimpleDateFormat formatter
= new SimpleDateFormat( "yyyy-MM-dd ");

String time =
formatter.format(date);//格式化数据

return time;

}

    //根据传入的日期计算距今多少天

    public static Integer getValidDays(Date date){
        int days=0;
        Date now=new Date();
        int i = now.compareTo(date);
        if (i>0) {
          return 0;
        }else{
          days = (int) Math.abs((now.getTime() - date.getTime())
          / (24 * 60 * 60 * 1000));
        }

       return days;
     }

public static
Long  getTime(){

Date dt= new
Date();

Long time= dt.getTime();

return time;

}

public static
Calendar convertToCalendar(String date) throws
ParseException{

Date d = new
SimpleDateFormat("yyyy-MM-dd").parse(date);

Calendar cal=Calendar.getInstance();

cal.setTime(d);

return cal;

}

public static void
main(String[] args) throws ParseException {

Calendar now = Calendar.getInstance();

System.out.println("年: " +
now.get(Calendar.YEAR));

System.out.println("月: " +
(now.get(Calendar.MONTH) + 1) + "");

System.out.println("日: " +
now.get(Calendar.DAY_OF_MONTH));

System.out.println("时: " +
now.get(Calendar.HOUR_OF_DAY));

System.out.println("分: " +
now.get(Calendar.MINUTE));

System.out.println("秒: " +
now.get(Calendar.SECOND));

System.out.println("当前时间毫秒数:" +
now.getTimeInMillis());

System.out.println("当前月的天数:" +
now.getActualMaximum(Calendar.DATE));

System.out.println(now.getTime());

//给定日期查看最大数

now.set(Calendar.YEAR,2002);

now.set(Calendar.MONTH,6);//7月

int   maxDate   =  
now.getActualMaximum(Calendar.DATE);

Date d = new
Date();

System.out.println(d);

SimpleDateFormat sdf = new
SimpleDateFormat("yyyy-MM-dd
HH:mm:ss");

String dateNowStr = sdf.format(d);

System.out.println("格式化后的日期:" +
dateNowStr);

String str = "2012-1-13 17:26:33";  //要跟上面sdf定义的格式一样

Date today = sdf.parse(str);

System.out.println("字符串转成日期:" +
today);

}

/**
* 计算两个日期之间的天数
*/
public class DateUtils {
public static Integer daysBetween(Date smdate, Date bdate){
try{
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
smdate=sdf.parse(sdf.format(smdate));
bdate=sdf.parse(sdf.format(bdate));
Calendar cal = Calendar.getInstance();
cal.setTime(smdate);
long time1 = cal.getTimeInMillis();
cal.setTime(bdate);
long time2 = cal.getTimeInMillis();
long between_days=(time2-time1)/(1000*3600*24);
return Integer.parseInt(String.valueOf(between_days));
}catch (Exception e){
throw new RuntimeException("The Date sub error");
}
}
/**
* 自然年加法
* @param date
* @return
*/
public static Date getAddDate(Date date,Integer year){
try{
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.YEAR, year);
date = calendar.getTime();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
String dateStr = simpleDateFormat.format(date);
Date parse = simpleDateFormat.parse(dateStr);
return parse;
}catch (Exception e){
throw new RuntimeException("The Date add error");
}
}     /**
     *
     * @param 要转换的毫秒数
     * @return 该毫秒数转换为 * days * hours * minutes * seconds 后的格式
     * @author fy.zhang
     */
    public static String formatDuring(long mss) {
        long days = mss / (1000 60 60 24);
        long hours = (mss % (1000 60 60 24)) / (1000 60 60);
        long minutes = (mss % (1000 60 60)) / (1000 60);
        long seconds = (mss % (1000 60)) / 1000;
        return days + " days " + hours + " hours " + minutes + " minutes "
                + seconds + " seconds ";
    }

Java 时间工具类的更多相关文章

  1. Java日期工具类,Java时间工具类,Java时间格式化

    Java日期工具类,Java时间工具类,Java时间格式化 >>>>>>>>>>>>>>>>>&g ...

  2. 小记Java时间工具类

    小记Java时间工具类 废话不多说,这里主要记录以下几个工具 两个时间只差(Data) 获取时间的格式 格式化时间 返回String 两个时间只差(String) 获取两个时间之间的日期.月份.年份 ...

  3. 超详细的Java时间工具类

    package com.td.util; import java.sql.Timestamp; import java.text.ParseException; import java.text.Pa ...

  4. java时间工具类

    在项目中,很多地方需要根据时间获取相应的数据,将时间格式化,或者时间比较等相关操作.一个良好的工具类不仅可以减少代码冗余,还能促进业务处理,加快进度. /** * @author: lxw * @Da ...

  5. 一个好的Java时间工具类DateTime

    此类的灵感来源于C# 虽然网上有什么date4j,但是jar太纠结了,先给出源码,可以继承到自己的util包中,作为一个资深程序员,我相信都有不少好的util工具类,我也希望经过此次分享,能带动技术大 ...

  6. JAVA时间工具类,在维护的项目里的

    package com.inspur.jobSchedule.util; import org.apache.commons.lang3.time.DateUtils; import org.apac ...

  7. java时间工具类,时间相互转换

    /* * @author XueWeiWei * @date 2019/8/26 16:22 */ package com.nps.utils; import java.text.ParseExcep ...

  8. JAVA时间工具类用法

    1.获得N天前的TIMESTAMP Calendar cl = Calendar.getInstance(); cl.add(Calendar.DAY_OF_YEAR, -7); Date date ...

  9. 代码片段:基于 JDK 8 time包的时间工具类 TimeUtil

    摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢! “知识的工作者必须成为自己时间的首席执行官.” 前言 这次泥瓦匠带来的是一个好玩的基于 JDK ...

随机推荐

  1. MyBatis传入多个参数 ,List集合

    一.单个参数: public List<XXBean> getXXBeanList(String xxCode); <select id="getXXXBeanList&q ...

  2. Linux Shell编程 循环语法

    for循环 for 循环是固定循环,也就是在循环时已经知道需要进行几次循环.有时也把 for 循环称为计数循环.语法: for 变量 in 值1 值2 值3… do 程序 done 在这种语法中,fo ...

  3. 案例:1 Ionic Framework+AngularJS+ASP.NET MVC WebApi Jsonp 移动开发

    落叶的庭院扫的一干二净之后,还要轻轻把树摇一下,抖落几片叶子,这才是Wabi Sabi的境界. 介绍:Ionic是移动框架,angularjs这就不用说了,ASP.Net MVC WebApi提供数据 ...

  4. ARC管理内存(一)

    相关概念 栈 当程序执行某个方法(或函数)时,会从内存中名为栈(stack)的区域分配一块内存空间,这块内存空间称为帧(frame).帧负责保存程序在方法内声明的变量的值.在方法内声明的变量称为局部变 ...

  5. Vue.js学习笔记 第五篇 事件处理

    监听事件 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <titl ...

  6. Kubernetes StatefulSets

    StatefulSets对于需要以下一项或多项的应用程序非常有用. 稳定,唯一的网络标识符. 稳定,持久的存储. 有序,优雅的部署和缩放. 有序,优雅的删除和终止. 有序的自动滚动更新. POD Id ...

  7. BZOJ 4154 kd-tree dfs序 + 二维空间的区间(矩阵)更新单点查找

    一开始没思路 感觉像是一个树形dp 然而不会 然后看了一眼题解就明白了 一个点的子树 用dfs序表示肯定是一个连续的区间 并且由于有子树的距离限制 可以转化为一个深度的区间 于是每个点都会有一个在二维 ...

  8. 添加vue调试工具vue-devtolls

    1.在使用脚手架vue-cli.js下载好node-modules 2.在node-modules目录下找的vue-devtools文件(如果没有可以用npm install vue-devtools ...

  9. 彻底弄清python的切片

    lis = range(100) # [0, 1, 2, 3, ..., 99] # 取前10个 lis[:10] # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] # 取后10个 l ...

  10. 刻录DVD.Win7系统盘(U盘)

    ZC:Win7x86的U盘安装盘做好之后,U盘 里面会留有 引导信息,在以后不想要它(引导信息)的时候 该如果将它删掉?直接普通的格式化 能行吗? ZC:(20180423)发现,UltraISO制作 ...