[原创]时间计算实用类

Junglesong 发表于 2008-06-04 11:48:20


import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class TimeUtil {
 private TimeUtil() {

 }
 
 public static String getCurrDateTime() {
  Date date = new Date();
  Format formatter = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss");
  return formatter.format(date);
 }

 public static String getCurrDate() {
  Date date = new Date();
  Format formatter = new SimpleDateFormat("yyyy.MM.dd");
  return formatter.format(date);
 }
 
 public static String getSimpleCurrDate() {
  Date date = new Date();
  Format formatter = new SimpleDateFormat("yyyy.M.d");
  return formatter.format(date);
 }

 public static String getCurrTime() {
  Date date = new Date();
  Format formatter = new SimpleDateFormat(".HH.mm.ss");
  return formatter.format(date);
 }

 public static String getCurrMonth() {
  Date date = new Date();
  Format formatter = new SimpleDateFormat("yyyy.M");
  return formatter.format(date);
 }

 public static Date getDate(String strMonth) {
  SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy.MM.dd");

  try {
   java.util.Date date = myFormatter.parse(strMonth);
   return date;
  } catch (Exception ex) {
   return null;
  }
 }

 public static long getDaysBetween(String startDate,String endDate){
  Date dStart=getDate(startDate);  
  Date dEnd=getDate(endDate);
  
  return (dEnd.getTime()-dStart.getTime())/(24 * 60 * 60 * 1000);
 }
 
 public static void main(String[] args){
  System.out.println(getDaysBetween("2008.06.05","2008.06.08"));
  System.out.println(getDaysBetween("2008.05.01","2008.06.01"));
  System.out.println(getDaysBetween("2007.01.01","2008.01.01"));
 }
}

收藏: QQ书签 del.icio.us 订阅: Google 抓虾

最新评论

发表评论

* 昵称

已经注册过? 请登录

新用户请先注册 以便能显示头像及追踪评论回复

Email
网址
* 评论
表情
 
 

分类小组论坛
杂谈, 娱乐、八卦, 文学、艺术, 体育, 旅游、同城, 象牙塔, 情感, 时尚、生活, 星座, 科技

请注意遵守中华人民共和国法律法规, 如威胁到本站生存, 将依法向有关部门报告, 同时本站的相关记录可能成为对您不利的证据.

相关法律法规
全国人大常委会关于维护互联网安全的决定
中华人民共和国计算机信息系统安全保护条例
中华人民共和国计算机信息网络国际联网管理暂行规定
计算机信息网络国际联网安全保护管理办法
计算机信息系统国际联网保密管理规定