博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java 时间工具类
阅读量:5273 次
发布时间:2019-06-14

本文共 6158 字,大约阅读时间需要 20 分钟。

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 "
;
    
}
 

 

 

转载于:https://www.cnblogs.com/jimmy-muyuan/p/5353100.html

你可能感兴趣的文章
linux下WPS的使用
查看>>
hdu 3938 并查集
查看>>
《深入分析Java Web技术内幕》读书笔记之JVM内存管理
查看>>
python之GIL release (I/O open(file) socket time.sleep)
查看>>
2015/8/4 告别飞思卡尔,抛下包袱上路
查看>>
软件开发与模型
查看>>
161017、SQL必备知识点
查看>>
kill新号专题
查看>>
MVC学习系列——Model验证扩展
查看>>
mysqladmin 修改和 初始化密码
查看>>
字符串
查看>>
vue2.x directive - 限制input只能输入正整数
查看>>
实现MyLinkedList类深入理解LinkedList
查看>>
自定义返回模型
查看>>
C#.NET 大型通用信息化系统集成快速开发平台 4.1 版本 - 客户端多网络支持
查看>>
HDU 4122
查看>>
Suite3.4.7和Keil u3自带fx2.h、fx2regs.h文件的异同
查看>>
打飞机游戏【来源于Crossin的编程教室 http://chuansong.me/account/crossincode 】
查看>>
[LeetCode] Merge Intervals
查看>>
【翻译自mos文章】当点击完 finishbutton后,dbca 或者dbua hang住
查看>>