zoukankan      html  css  js  c++  java
  • java时间日期处理


    /**
    * <p>
    * Title: 工具类
    * </p>
    * <p>
    * Description: 用来处理时间日期字符串等
    * </p>
    * <p>
    * Copyright: (C) 2006.11 常用方法,需要者随便拿去^-^
    * </p>
    *
    * @author weiking
    * @version 1.0
    */

    public class UtilTool {

    public UtilTool() {
    super();
    }

    /**
    * 取当前打印日期
    *
    * @return
    * @throws java.lang.Exception
    */

    public String getPrintDate() {
    String printDate =
    "";
    Calendar calendar = new GregorianCalendar();
    calendar.setTime(new Date());
    printDate += calendar.get(Calendar.YEAR) +
    "年";
    printDate += (calendar.get(Calendar.MONTH) + 1) +
    "月";
    printDate += calendar.get(Calendar.DATE) +
    "日";
    return printDate;
    }

    /**
    * 将指定的日期字符串转化为日期对象
    *
    * @param dateStr
    *             日期字符串
    * @return java.util.Date
    */

    public static Date getDate(String dateStr, String format) throws Exception {

    if (dateStr == null || format == null) {
    throw new Exception(
    "数据类型异常" + dateStr + "|" + format);
    }

    SimpleDateFormat df = new SimpleDateFormat(format);

    try {
    Date date = df.parse(dateStr);
    return date;
    } catch (Exception ex) {
    return null;
    }
    }

    /**
    * 将指定日期转换为 Timestamp
    *
    * @param date
    *             指定日期格式为 "20030908"
    * @return Timestamp
    * @throws Exception
    */

    public static Timestamp getTimestamp(String dateStr) throws Exception {
    SimpleDateFormat sdf = new SimpleDateFormat(
    "yyyy-MM-dd 00:00:00.000");
    return Timestamp.valueOf(sdf.format(getDate(dateStr,
    "yyyyMMdd")));
    }

    /**
    * 从指定Timestamp中得到相应的日期
    *
    * @param datetime
    *             指定的Timestamp
    * @return 日期 "2003-09-08"
    */

    public String getDateFromDateTime(Timestamp datetime) {
    SimpleDateFormat sdf = new SimpleDateFormat(
    "yyyyMMdd");
    return sdf.format(datetime).toString();
    }

    /**
    * 得到当前时间的时间戳
    *
    * @return 当前时间戳
    */

    public Timestamp getNowTimestamp() {
    long curTime = System.currentTimeMillis();
    return new Timestamp(curTime);
    }

    }

    原文:http://www.zhuoda.org/weiking/72133.html

  • 相关阅读:
    终端移植
    优化工具库
    Linux环境下修改MySQL数据库存储引擎
    Too many open files故障解决一例
    MySQL故障处理一例_Another MySQL daemon already running with the same unix socket
    设置VMWare虚拟机使拷贝虚拟机后固定原有的IP地址
    Eclipse安装TestNG插件
    Node“getTextContent() is undefined for the type Node”处理办法
    使用Oracle SQL Developer迁移MySQL至Oracle数据库
    RHEL5.6环境下yum安装MySQL
  • 原文地址:https://www.cnblogs.com/macula7/p/1960486.html
Copyright © 2011-2022 走看看