获取本地日期与时间

public String  getCalendar() {
    @SuppressLint("SimpleDateFormat")
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    Date dt = new Date();
    return sdf.format(dt);
}

//SimpleDateFormat 时间格式

//y 年 MM 月 dd 日 

//D 一年的第几天 W 一个月的第几星期 w 一年的第几星期 k时 z时区

//HH 小时(24小时制) hh小时(12小时制) mm 分 ss 秒 SS 毫秒 E 星期 

//a 上午/下午

计算相隔天数

/**
 * 获得天数差
 * @param begin
 * @param end
 * @return
 */
public long getDayDiff(Date begin, Date end){
    long day = 1;
    if(end.getTime() < begin.getTime()){
        day = -1;
    }else if(end.getTime() == begin.getTime()){
        day = 1;
    }else {
        day += (end.getTime() - begin.getTime())/(24 * 60 * 60 * 1000) ;
    }
    return day-1;
}

Date date=new Date(2017-1900,11,1,0,30);

Date date2=new Date(2018-1900,11,1,0,30);

getDayDiff(date,date2)

计算时间差

   public static String getTimeDiffer(String date,String date2,String str){
        long diff,days,hours,minutes;
        @SuppressLint("SimpleDateFormat")
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        try
        {
            Date d1 = df.parse(date);
            Date d2 = df.parse(date2);
            if(d1.getTime()>d2.getTime()) {
                diff = d1.getTime() - d2.getTime();//这样得到的差值是毫秒级别
                days = diff / (1000 * 60 * 60 * 24);

                hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
                minutes = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);
            }else{
                return "已到期";
            }
            if(days==0){
                if(hours==0){
                    return minutes+"分钟";
                }
            }
            if(str.equals("精确")){
                return ""+days+"天"+hours+"小时"+minutes+"分钟";
            }
            return ""+days+"天"+hours+"小时";
        }catch (Exception ignored){
        }
        return "";
    }

判断日期是否在指定日期

  public static int getTimeCompareSize2(String startTime, String endTime){
        int i=0;
        @SuppressLint("SimpleDateFormat")
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");//年-月-日 时-分
        try {
            Date date1 = dateFormat.parse(startTime);//开始时间
            Date date2 = dateFormat.parse(endTime);//结束时间
            if (date2.getTime()<date1.getTime()){
                i= 1;// 结束时间小于开始时间
            }else if (date2.getTime()==date1.getTime()){
                i= 2;//开始时间与结束时间相同
            }else if (date2.getTime()>date1.getTime()){
                i= 3;//结束时间大于开始时间
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return  i;
    }

获取网络时间与日期

private void getNetTime() {
     URL url = null;
     try {
          url = new URL("http://www.baidu.com");
          URLConnection uc = url.openConnection();
          uc.connect();
          long ld = uc.getDate(); //取得网站日期时间
          @SuppressLint("SimpleDateFormat")
          DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
          Calendar calendar = Calendar.getInstance();
          calendar.setTimeInMillis(ld);
          final String format = formatter.format(calendar.getTime());
          boottom_tiem.setText(format);
     } catch (Exception e) {
         e.printStackTrace();
     }
}

Android 时间与日期操作类的更多相关文章

  1. Android随笔之——Android时间、日期相关类和方法

    今天要讲的是Android里关于时间.日期相关类和方法.在Android中,跟时间.日期有关的类主要有Time.Calendar.Date三个类.而与日期格式化输出有关的DateFormat和Simp ...

  2. 使用日期操作类(Calendar)获得几秒、几分钟、几小时之前的时间

    public String dealDate(String case_time){ // 日期操作类 Calendar calendar = Calendar.getInstance(); // 当前 ...

  3. JAVA笔记10__Math类、Random类、Arrays类/日期操作类/对象比较器/对象的克隆/二叉树

    /** * Math类.Random类.Arrays类:具体查JAVA手册...... */ public class Main { public static void main(String[] ...

  4. 菜鸡的Java笔记 日期操作类

    日期操作类        Date 类与 long 数据类型的转换        SimpleDateFormat 类的使用        Calendar 类的使用                如 ...

  5. Lua库之时间和日期操作

    Lua库之时间和日期操作 (2010-02-07 18:41:20) 转载▼ os.time() <== 返回当前系统的日历时间os.date() <== 返回本地化的时间字符串,这里是& ...

  6. 日期操作类--Date类

    Date-API ava.util包提供了Date类来封装当前的日期和时间.Date类提供两个构造函数来实例化Date对象.第一个构造函数使用当前日期和时间来初始化对象. Date( ) 第二个构造函 ...

  7. 日期操作类--GregorianCalendar类

    GregorianCalendar--API JavaTM Platform Standard Ed. 6 GregorianCalendar类 Calendar类实现了公历日历,GregorianC ...

  8. 日期操作类--SimpleDateFormat类

    使用SimpleDateFormat格式化日期 SimpleDateFormat是一个以语言环境敏感的方式来格式化和分析日期的类.SimpleDateFormat允许你选择任何用户自定义日期时间格式来 ...

  9. 日期操作类--DateFormat类

    简单的DateFormat格式化编码 时间模式字符串用来指定时间格式.在此模式中,所有的ASCII字母被保留为模式字母,定义如下: 字母 描述 示例 G 纪元标记 AD y 四位年份 2001 M 月 ...

随机推荐

  1. 恭喜"微微软"喜当爹,Github嫁入豪门。

    今天是 Github 嫁入豪门的第 2 天,炒得沸沸扬扬的微软 Github 收购事件于昨天(06月04日)尘埃落定,微软最终以 75 亿美元正式收购 Github. 随后,Gitlab 趁势带了一波 ...

  2. Docker 部署Django项目

    使用docker部署django项目也很简单,挺不错,分享下 环境 默认你已安装好docker环境 django项目大概结构 (p3s) [root@opsweb]# tree opsweb opsw ...

  3. 个人博客搭建( wordpress )

    最近同学买了一台虚机( centos7 系统 ).一个域名,让帮忙搭一个个人博客.本着简单快捷,个人博客采用 wordpress 来搭建.为了以后博客系统的迁移方便,使用 docker 来安装 wor ...

  4. 通过Postman进行post请求时传递X-XSRF-TOKEN

    前言介绍 这段时间一个项目后端用的是laravel.在写API接口时通过Postman6进行测试.但是在测试后形式的接口时laravel自带了CSRF验证机制.这就很尴尬了... 所以我们的目的在使用 ...

  5. IO流查找文件然后写入TXT文档

    今天领导让分析日志,把日志中所有登录过的员工信息都拿出来.于是.把日志摘下来谢了这段代码 import java.io.BufferedReader;import java.io.BufferedWr ...

  6. websphere静态文件转发出错问题 SimpleFileServlet

    ERROR DESCRIPTION: Using a RequestDispatcher to explicitly forward to the WebContainer's SimpleFileS ...

  7. tcp 三次握手 四次挥手

    TCP协议中的三次握手和四次挥手 建立TCP需要三次握手才能建立,而断开连接则需要四次挥手. 三次握手,建立连接 首先Client发送连接请求报文,Server端接收连接后回复ACK报文,并为这次连接 ...

  8. python计算数组中对象出现的次数并且按照字典输出

    解决的问题如题,如果对Python不是很熟悉,解决的办法可能如下: test_array=[1,2,3,1,2,3]; def count_object1(array): result_obj={} ...

  9. 001. Asp.Net Routing与MVC 之(基础知识):URL

    URL(Uniform Resoure Locator:统一资源定位器)是WWW页的绝对地址.URL地址格式排列为:scheme://host:port/path. 例如 http://www.zn. ...

  10. Android_Fragment和Activity之间的通信

    Fragment 的生命周期是随着activity变化而变化的. 如果activity要给在运行的时候给fragment传人数据则需要fragment实现一个自定义的接口,并且实现接口里面的方法,在a ...