java时间类简单整合

package com.rengh.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class DateUtils {

    /** 格式化时间 */
    public static String formatTime(long showTime) {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(Long
                .valueOf(showTime));
    }

    /** 格式化时间 */
    public static String formatTime(String showTime) {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(Long
                .valueOf(showTime));
    }

    /** 获取时间的毫秒数 */
    public static long getMillis() throws ParseException {
        return System.currentTimeMillis();
    }

    /** 获取指定时间的毫秒数,参数格式: yyyy-MM-dd HH:mm:ss,返回null代表计算失败 */
    public static long getMillis(String dstr) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = sdf.parse(dstr);
        long s1 = date.getTime();
        return s1;
    }

    /** 返回 指定时间的毫秒数 减 当前日期的毫秒数;参数格式: yyyy-MM-dd HH:mm:ss,返回null代表计算失败 */
    public static long getMillisDistanceNow(String dstr) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = sdf.parse(dstr);
        long s1 = date.getTime();
        long s2 = System.currentTimeMillis();
        return s1 - s2;
    }

    /** 返回格式:2015-07-08 */
    public static String getTodayDate() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        String now = sdf.format(Long.valueOf(System.currentTimeMillis()));
        return now;
    }

    /** 返回格式:2015-07-08 08:00:00 */
    public static String getTodayTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        String now = sdf.format(Long.valueOf(System.currentTimeMillis()));
        return now;
    }

    /** 返回格式:2015-07-08 */
    public static String getTomorrowDate() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        String date = sdf
                .format(Long.valueOf(System.currentTimeMillis() + 1 * 24 * 60 * 60 * 1000));
        return date;
    }

    /** 返回格式:2015-07-08 00:00:00 */
    public static String getTomorrowTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        String now = sdf.format(Long.valueOf(System.currentTimeMillis() + 1 * 24 * 60 * 60 * 1000));

        long zeroHourMillis = 0;
        try {
            zeroHourMillis = getMillis(now + " " + "00:00:00");
        } catch (ParseException e) {
            e.printStackTrace();
        }

        sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        String time = sdf.format(Long.valueOf(zeroHourMillis));
        return time;
    }

    /** 比较日期的大小, */
    public static int compareDate(String date1, String date2) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        Date d1 = null;
        Date d2 = null;
        try {
            d1 = sdf.parse(date1);
            d2 = sdf.parse(date2);
        } catch (ParseException e) {
            e.printStackTrace();
            return -1;
        }
        return d1.compareTo(d2);
    }

    /** 比较日期与当前日期的大小, */
    public static int compareDate(String dateStr) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        String now = sdf.format(Long.valueOf(System.currentTimeMillis()));
        Date date = null;
        Date nowdate = null;
        try {
            date = sdf.parse(dateStr);
            nowdate = sdf.parse(now);
        } catch (ParseException e) {
            e.printStackTrace();
            return -1;
        }
        return nowdate.compareTo(date);
    }
}
时间: 2024-08-27 06:58:00

java时间类简单整合的相关文章

java时间类简单总结

java时间类(Data类) 1.Data类(没有考虑到国际化,好多方法已过时java.util.Data包中) 父类(是类不是接口含有直接子类3个):  日期格式为:年月日时分秒(不包含毫秒部分) 两种方式创建:带参构造方法(long型参数:Data(long log)),不带参 常用方法:after(),before(),compareTo()等方法. 直接子类:java.sql.Data(用于提供数据库日期的表示)   日期格式为:年月日(只有日期,没有时间) 该类构造函数必须是带参数的(

java时间类的使用

日期的使用: 1.创建日期 2.日期格式化显示 3.日期的转换 4.两个日期的大小比较.日期的加减   一些知识:       全球24个时区的瓜分       格林威治标准时间GMT 世界协调时间(世界标准时间)UTC       夏日节约时间DST       CST时间 CST却同时可以代表如下 4 个不同的时区: Central Standard Time (USA) UT-6:00 Central Standard Time (Australia) UT+9:30 China Stan

Java Calendar类简单用法

我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3832307.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验. 初始化 Calendar calendar = new GregorianCalendar(); set方法 calendar.set(Calendar.YEAR, year); get方法 int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);

java 时间类Date和Calendar

Java的时间类经常用到,主要是两个类Date和Calendar,分别做下介绍. 一.Date 1.在 package java.util 类定义如下: public class Date implements java.io.Serializable, Cloneable, Comparable<Date>{} 类 Date 表示特定的瞬间,精确到毫秒. 在 JDK 1.1 之前,类 Date 有两个其他的函数.它允许把日期解释为年.月.日.小时.分钟和秒值.它也允许格式化和解析日期字符串.

java时间类API

一.jdk8之前的日期时间的API测试: 1.System了中currentTimeMIllis()://返回从1970年到现在的总毫秒数 2.java.util.Date和子类java.sql.Date   //返回当前日期,后者是兼容数据库类型的日期 比如:给一个Date类的日期,可以先使用getTime()转化为毫秒数,然后作为参数传递给创建java.sql.Date的实例化作为参数,那么输出该实例化对象就是数据库兼容类型的日期. 3.simpleDateFormat  //对日期Date

Java 时间类

一.Date类 Date:表示特定的瞬间,精确到毫秒构造方法:     (1)public Date():根据当前的默认毫秒值创建日期对象     (2)public Date(long date):根据给定的毫秒值创建日期对象成员方法:     (1)public long getTime():获取时间,返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数.     (2)public long setTime(long time):设置时间,表示

Java时间类Date与Calendar的区别与使用 &#350964;

原文: http://blog.gqylpy.com/gqy/464 置顶:来自一名75后老程序员的武林秘籍--必读(博主推荐) 来,先呈上武林秘籍链接:http://blog.gqylpy.com/gqy/401/ 你好,我是一名极客!一个 75 后的老工程师! 我将花两分钟,表述清楚我让你读这段文字的目的! 如果你看过武侠小说,你可以把这个经历理解为,你失足落入一个山洞遇到了一位垂暮的老者!而这位老者打算传你一套武功秘籍! 没错,我就是这个老者! 干研发 20 多年了!我也年轻过,奋斗过!我

Java:字符串类简单的正则表达式

1 class Test 2 { 3 public static void main(String[] args) 4 { 5 String str = "xia..as....yuan.com"; 6 7 //判断 8 boolean b1 = str.endsWith(".com"); 9 boolean b2 = str.startsWith("xia"); 10 System.out.println(b1); 11 System.out.

Java中常见时间类的使用

模拟场景针对于常用的操作API,比如流操作(字符流.字节流),时间操作等,仅仅了解概念性的定义终究是无法了解该类的用途和使用方式:这种情况在使用的时候便一脸茫然,脑海中映射不到对应的知识点.本篇博客将通过一个简单的应用场景来体会这两个类的使用. 项目开发阶段,有一个关于下单发货的需求:如果今天下午三点前进行下单,那么发货时间是明天如果今天下午三点后机型下单,那么发货时间是后天如果被确定的发货时间是周日的话,那么在此时间上再加一天为最终发货时间 牛刀小试( java 原始)整体逻辑:主函数创建下单