Android-DateTimeAndroidUtil-工具类
DateTimeAndroidUtil-工具类 是关于时间日前相关的公用方法;
package liudeli.mynetwork01.utils; import android.util.Log; import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone; /**
* @Author Liudeli
* @Describe:时间相关工具类
*/
public class DateTimeAndroidUtil { private DateTimeAndroidUtil(){} /**
* 枚举日期格式
*/
public enum DatePattern{
/**
* 格式:"yyyy-MM-dd HH:mm:ss"
*/
ALL_TIME{public String getValue(){return "yyyy-MM-dd HH:mm:ss";}},
/**
* 格式:"yyyy-MM"
*/
ONLY_MONTH{public String getValue(){return "yyyy-MM";}},
/**
* 格式:"yyyy-MM-dd"
*/
ONLY_DAY{public String getValue(){return "yyyy-MM-dd";}},
/**
* 格式:"yyyy-MM-dd HH"
*/
ONLY_HOUR{public String getValue(){return "yyyy-MM-dd HH";}},
/**
* 格式:"yyyy-MM-dd HH:mm"
*/
ONLY_MINUTE{public String getValue(){return "yyyy-MM-dd HH:mm";}},
/**
* 格式:"MM-dd"
*/
ONLY_MONTH_DAY{public String getValue(){return "MM-dd";}},
/**
* 格式:"MM-dd HH:mm"
*/
ONLY_MONTH_SEC{public String getValue(){return "MM-dd HH:mm";}},
/**
* 格式:"HH:mm:ss"
*/
ONLY_TIME{public String getValue(){return "HH:mm:ss";}},
/**
* 格式:"HH:mm"
*/
ONLY_HOUR_MINUTE{public String getValue(){return "HH:mm";}};
public abstract String getValue();
} /**
* 获取当前时间
* @return 返回当前时间,格式2017-05-04 10:54:21
*/
public static String getNowDate(DatePattern pattern){
String dateString = null;
Calendar calendar = Calendar.getInstance();
Date dateNow = calendar.getTime();
SimpleDateFormat sdf = new SimpleDateFormat(pattern.getValue(),Locale.CHINA);
dateString = sdf.format(dateNow);
return dateString;
} /**
* 将一个日期字符串转换成Data对象
* @param dateString 日期字符串
* @param pattern 转换格式
* @return 返回转换后的日期对象
*/
public static Date stringToDate(String dateString, DatePattern pattern){
Date date = null;
SimpleDateFormat sdf = new SimpleDateFormat(pattern.getValue(),Locale.CHINA);
try {
date = sdf.parse(dateString);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
} /**
* 将date转换成字符串
* @param date 日期
* @param pattern 日期的目标格式
* @return
*/
public static String dateToString(Date date, DatePattern pattern){
String string = "";
SimpleDateFormat sdf = new SimpleDateFormat(pattern.getValue(), Locale.CHINA);
string = sdf.format(date);
return string;
} /**
* 获取指定日期周几
* @param date 指定日期
* @return
* 返回值为: "周日", "周一", "周二", "周三", "周四", "周五", "周六"
*/
public static String getWeekOfDate(Date date){
String[] weekDays = { "周日", "周一", "周二", "周三", "周四", "周五", "周六" };
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int week = calendar.get(Calendar.DAY_OF_WEEK) - 1;
if (week < 0)
week = 0;
return weekDays[week];
} /**
* 获取指定日期对应周几的序列
* @param date 指定日期
* @return 周一:1 周二:2 周三:3 周四:4 周五:5 周六:6 周日:7
*/
public static int getIndexWeekOfDate(Date date){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int index = calendar.get(Calendar.DAY_OF_WEEK);
if(index == 1){
return 7;
}else{
return --index;
}
} /**
* 返回当前月份
* @return
*/
public static int getNowMonth(){
Calendar calendar = Calendar.getInstance();
return calendar.get(Calendar.MONTH) + 1;
} /**
* 获取当前月号
* @return
*/
public static int getNowDay(){
Calendar calendar = Calendar.getInstance();
return calendar.get(Calendar.DATE);
} /**
* 获取当前年份
* @return
*/
public static int getNowYear(){
Calendar calendar = Calendar.getInstance();
return calendar.get(Calendar.YEAR);
} /**
* 获取本月份的天数
* @return
*/
public static int getNowDaysOfMonth(){
Calendar calendar = Calendar.getInstance();
return daysOfMonth(calendar.get(Calendar.YEAR),calendar.get(Calendar.DATE) + 1);
} /**
* 获取指定月份的天数
* @param year 年份
* @param month 月份
* @return 对应天数
*/
public static int daysOfMonth(int year,int month){
switch(month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
case 4:
case 6:
case 9:
case 11:
return 30;
case 2:
if((year % 4 ==0 && year % 100 == 0) || year % 400 != 0){
return 29;
}else{
return 28;
}
default:
return -1;
}
} /**
* 获取当前时间
* @return
*/
public static String getNowTime(){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date(System.currentTimeMillis());
return simpleDateFormat.format(date);
} /**
* 获取当前时间
* @return
*/
public static String getThisTiem() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");// HH:mm:ss
// 获取当前时间
Date date = new Date(System.currentTimeMillis());
return simpleDateFormat.format(date);
} /**
* 获取时间戳
*
* @return 获取时间戳
*/
public static String getTimeString() {
SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
Calendar calendar = Calendar.getInstance();
return df.format(calendar.getTime());
} /**
* 获取时间戳
*
* @return 获取时间戳
*/
public static String getTimeString2() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar calendar = Calendar.getInstance();
return df.format(calendar.getTime());
} /**
* 时间转换为时间戳
* @param time:需要转换的时间
* @return
*/
public static String dateToStamp(String time) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = null;
try {
date = simpleDateFormat.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
long ts = date.getTime();
return String.valueOf(ts);
} /**
* 时间戳转换为字符串
* @param time:时间戳
* @return
*/
public static String times(String time) {
SimpleDateFormat sdr = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分");
@SuppressWarnings("unused")
long lcc = Long.valueOf(time);
int i = Integer.parseInt(time);
String times = sdr.format(new Date(i * 1000L));
return times; }
/**
*获取距现在某一小时的时刻
* @param hour hour=-1为上一个小时,hour=1为下一个小时
* @return
*/
public static String getLongTime(int hour){
Calendar c = Calendar.getInstance(); // 当时的日期和时间
int h; // 需要更改的小时
h = c.get(Calendar.HOUR_OF_DAY) - hour;
c.set(Calendar.HOUR_OF_DAY, h);
SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
Log.v("time",df.format(c.getTime()));
return df.format(c.getTime());
}
/**
* 比较时间大小
* @param str1:要比较的时间
* @param str2:要比较的时间
* @return
*/
public static boolean isDateOneBigger(String str1, String str2) {
boolean isBigger = false;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
Date dt1 = null;
Date dt2 = null;
try {
dt1 = sdf.parse(str1);
dt2 = sdf.parse(str2);
} catch (ParseException e) {
e.printStackTrace();
}
if (dt1.getTime() > dt2.getTime()) {
isBigger = true;
} else if (dt1.getTime() < dt2.getTime()) {
isBigger = false;
}
return isBigger;
} /**
* 当地时间 ---> UTC时间
* @return
*/
public static String Local2UTC(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("gmt"));
String gmtTime = sdf.format(new Date());
return gmtTime;
} /**
* UTC时间 ---> 当地时间
* @param utcTime UTC时间
* @return
*/
public static String utc2Local(String utcTime) {
SimpleDateFormat utcFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//UTC时间格式
utcFormater.setTimeZone(TimeZone.getTimeZone("UTC"));
Date gpsUTCDate = null;
try {
gpsUTCDate = utcFormater.parse(utcTime);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat localFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//当地时间格式
localFormater.setTimeZone(TimeZone.getDefault());
String localTime = localFormater.format(gpsUTCDate.getTime());
return localTime;
}
}
Android-DateTimeAndroidUtil-工具类的更多相关文章
- 53. Android常用工具类
主要介绍总结的Android开发中常用的工具类,大部分同样适用于Java.目前包括HttpUtils.DownloadManagerPro.ShellUtils.PackageUtils.Prefer ...
- Android 常见工具类封装
1,MD5工具类: public class MD5Util { public final static String MD5(String s) { char hexDigits[] = { '0' ...
- 【转】Android常用工具类
主要介绍总结的Android开发中常用的工具类,大部分同样适用于Java. 目前包括HttpUtils.DownloadManagerPro.ShellUtils.PackageUtils.Prefe ...
- Android基础工具类重构系列一Toast
前言: 一直在考虑写一下Android实际项目中的一些总结,翻看CSDN博客,上一篇已经是一年多曾经. 本系列定位Android基础工具类重构.旨在记录实际项目中经经常使用到的一些工具类,比方Toas ...
- (转载)android 一些工具类汇总
android 一些工具类汇总 作者:曾田生z 字体:[增加 减小] 类型:转载 时间:2016-08-14我要评论 本文给大家汇总介绍了一些常用的Android工具类,非常的简单实用,有需要的小伙伴 ...
- 随笔分类 - Android之工具类
Android之文件搜索工具类 /** * @detail 搜索sdcard文件 * @param 需要进行文件搜索的目录 * @param 过滤搜索文件类型 */ private void sear ...
- Android 系统工具类SystemUtils
包含的功能有: 获取系统中所有APP应用.获取用户安装的APP应用.根据包名和Activity启动类查询应用信息.跳转到WIFI设置.WIFI网络开关.移动网络开关.GPS开关 当前若关则打开 当前若 ...
- Android Sqlite 工具类封装
鉴于经常使用 Sqlite 数据库做数据持久化处理,进行了一点封装,方便使用. 该封装类主要支持一下功能 支持多用户数据储存 支持 Sqlite数据库升级 支持传入 Sql 语句建表 支持 SQLit ...
- Android 常用工具类之SPUtil,可以修改默认sp文件的路径
参考: 1. 利用Java反射机制改变SharedPreferences存储路径 Singleton1900 2. Android快速开发系列 10个常用工具类 Hongyang import ...
- Android常见工具类封装
MD5加密 import android.annotation.SuppressLint; import java.security.MessageDigest; public class MD5 { ...
随机推荐
- emacs之配置5,窗口位置和大小
emacsConfig/window-setting.el ;设置窗口位置 ( ) ;设置宽和高 () (if (eq system-type 'darwin) ()) (if (eq system- ...
- eclipse 的安装和汉化
第一步:直接百度搜索eclipse,第一个就是官方网站 第二步:点击DOWNLOAD 64BIT进入下载页面 第三步:点击DOWNLOAD进行下载 If the download doesn't st ...
- 字符串作为freemarker模板的简单实现例子
本文转载自:http://blog.csdn.net/5iasp/article/details/27181365 package com.test.demo; import java.io.IOEx ...
- Python的设计哲学
Beautiful is better than ugly. 优美胜于丑陋 Explicit is better than implicit. 明了胜于晦涩 Simple is better than ...
- Java——复制txt文件
将源文件复制到目标文件,同时输出源文件内容,需要提供一个源文件和目标文件路径参数(如果不存在则自动创建) public static void copyTxt(String sourceFile, S ...
- PowerDesigner 生成的脚本取掉双引号
建模工具PowerDesigner http://www.cnblogs.com/mcgrady/archive/2013/05/25/3098588.html 默认导出在带双引号,表名称后期使用的时 ...
- Selection II
[Selection II] 1.上.下.左.右键可以移动Selection 1个像素.按住Shift键,可以一次移动10个像素. 2.Add Selection模式的快捷键是Shift,Sub Se ...
- Memcached在Linux环境下的使用详解http://blog.51cto.com/soysauce93/1737161
大纲 一.什么是memcached 二.memcached特性 三.memcached存储方式 四.memcached安装与配置 五.memcached结合php 六.Nginx整合memcached ...
- TensorMask
原文地址:https://arxiv.org/pdf/1903.12174.pdf 论文阅读:http://www.zhenzhujue.cn/article-36456-1.html https:/ ...
- 6-Ubuntu与Windows不能相互复制
在安装Ubuntu系统后发现与Windows系统的文件不能相互复制,网上查了很多教程,发现都是不能用的,能实现的方法如下所示: 第一步: sudo apt-get autoremove open-vm ...