date java 时间相关

Java代码   收藏代码
  1.     DateFormat   df   =   new   SimpleDateFormat( "yyyy-MM-dd   HH:mm:ss ");  
  2.         System.out.println(df.format(new Date()));   
  3. String time =df.format(new Date());   
  4.   
  5.   
  6. DateFormat   df   =   new   SimpleDateFormat( "yyyy-MM-dd   HH:mm:ss ");  
  7.         String currenttime =df.format(new Date());   
  8.         Timestamp createtime = Timestamp.valueOf(currenttime);    



Java代码   收藏代码
  1. Date time = new Date();  
  2.         int createtime = (int) time.getTime();  



Java代码   收藏代码
  1. 用DateFormat.getDateInstance().format(new Date());转换成如2008-05-04 09:48:17.687的日期格式就可以进行相减了。  












Java代码   收藏代码
  1. package com.enation.framework.util;  
  2.   
  3. import java.text.SimpleDateFormat;  
  4. import java.util.Calendar;  
  5. import java.util.Date;  
  6.   
  7. /** 
  8.  * 日期相关的操作 
  9.  * @author Dawei 
  10.  *   
  11.  */  
  12.   
  13. public class DateUtil {  
  14.   
  15.     /** 
  16.      * 将一个字符串转换成日期格式 
  17.      * @param date 
  18.      * @param pattern 
  19.      * @return 
  20.      */  
  21.     public static Date toDate(String date, String pattern) {  
  22.         if((""+date).equals("")){  
  23.             return null;  
  24.         }  
  25.         if(pattern == null){  
  26.             pattern = "yyyy-MM-dd";  
  27.         }  
  28.         SimpleDateFormat sdf = new SimpleDateFormat(pattern);  
  29.         Date newDate = new Date();  
  30.         try {  
  31.             newDate = sdf.parse(date);  
  32.         } catch (Exception ex) {  
  33.             ex.printStackTrace();  
  34.         }  
  35.         return newDate;  
  36.     }  
  37.       
  38.     /** 
  39.      * 把日期转换成字符串型 
  40.      * @param date 
  41.      * @param pattern 
  42.      * @return 
  43.      */  
  44.     public static String toString(Date date, String pattern){  
  45.         if(date == null){  
  46.             return "";  
  47.         }  
  48.         if(pattern == null){  
  49.             pattern = "yyyy-MM-dd";  
  50.         }  
  51.         String dateString = "";  
  52.         SimpleDateFormat sdf = new SimpleDateFormat(pattern);  
  53.         try {  
  54.             dateString = sdf.format(date);  
  55.         } catch (Exception ex) {  
  56.             ex.printStackTrace();  
  57.         }  
  58.         return dateString;  
  59.     }  
  60.   
  61.       
  62.       
  63.     /** 
  64.      * 获取上个月的开始结束时间 
  65.      * @return 
  66.      */  
  67.     public static String[] getLastMonth() {  
  68.            // 取得系统当前时间  
  69.            Calendar cal = Calendar.getInstance();  
  70.            int year = cal.get(Calendar.YEAR);  
  71.            int month = cal.get(Calendar.MONTH) + 1;  
  72.              
  73.            // 取得系统当前时间所在月第一天时间对象  
  74.            cal.set(Calendar.DAY_OF_MONTH, 1);  
  75.              
  76.            // 日期减一,取得上月最后一天时间对象  
  77.            cal.add(Calendar.DAY_OF_MONTH, -1);  
  78.              
  79.            // 输出上月最后一天日期  
  80.            int day = cal.get(Calendar.DAY_OF_MONTH);  
  81.   
  82.            String months = "";  
  83.            String days = "";  
  84.   
  85.            if (month > 1) {  
  86.             month--;  
  87.            } else {  
  88.             year--;  
  89.             month = 12;  
  90.            }  
  91.            if (!(String.valueOf(month).length() > 1)) {  
  92.             months = "0" + month;  
  93.            } else {  
  94.             months = String.valueOf(month);  
  95.            }  
  96.            if (!(String.valueOf(day).length() > 1)) {  
  97.             days = "0" + day;  
  98.            } else {  
  99.             days = String.valueOf(day);  
  100.            }  
  101.            String firstDay = "" + year + "-" + months + "-01";  
  102.            String lastDay = "" + year + "-" + months + "-" + days;  
  103.   
  104.            String[] lastMonth = new String[2];  
  105.            lastMonth[0] = firstDay;  
  106.            lastMonth[1] = lastDay;  
  107.   
  108.          //  System.out.println(lastMonth[0] + "||" + lastMonth[1]);  
  109.            return lastMonth;  
  110.         }  
  111.       
  112.       
  113.     /** 
  114.      * 获取当月的开始结束时间 
  115.      * @return 
  116.      */  
  117.     public static String[] getCurrentMonth() {  
  118.            // 取得系统当前时间  
  119.            Calendar cal = Calendar.getInstance();  
  120.            int year = cal.get(Calendar.YEAR);  
  121.            int month = cal.get(Calendar.MONTH)+1 ;  
  122.              
  123.            // 取得系统当前时间所在月第一天时间对象  
  124.            cal.set(Calendar.DAY_OF_MONTH, 1);  
  125.              
  126.            // 日期减一,取得上月最后一天时间对象  
  127.            cal.add(Calendar.DAY_OF_MONTH, -1);  
  128.              
  129.            // 输出上月最后一天日期  
  130.            int day = cal.get(Calendar.DAY_OF_MONTH);  
  131.   
  132.            String months = "";  
  133.            String days = "";  
  134.   
  135.   
  136.            if (!(String.valueOf(month).length() > 1)) {  
  137.             months = "0" + month;  
  138.            } else {  
  139.             months = String.valueOf(month);  
  140.            }  
  141.            if (!(String.valueOf(day).length() > 1)) {  
  142.             days = "0" + day;  
  143.            } else {  
  144.             days = String.valueOf(day);  
  145.            }  
  146.            String firstDay = "" + year + "-" + months + "-01";  
  147.            String lastDay = "" + year + "-" + months + "-" + days;  
  148.   
  149.            String[] currentMonth = new String[2];  
  150.            currentMonth[0] = firstDay;  
  151.            currentMonth[1] = lastDay;  
  152.   
  153.          //  System.out.println(lastMonth[0] + "||" + lastMonth[1]);  
  154.            return currentMonth;  
  155.         }  
  156.           
  157.       
  158.       
  159.     public static int getDateline(){  
  160.           
  161.         return (int)(System.currentTimeMillis()/1000);  
  162.     }  
  163.       
  164.     public static int getDateline(String date){  
  165.         return (int)(toDate(date, "yyyy-MM-dd").getTime()/1000);  
  166.     }  
  167. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值