打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Android中日期、时间戳之间的转换工具类

以下是我目前用到过的,后期如果遇到其他需求也会补上的,各位朋友遇到了什么转换也可以提示。

  1. package com.example.zyt.myutilslist.utils;
  2. import android.util.Log;
  3. import java.text.ParseException;
  4. import java.text.SimpleDateFormat;
  5. import java.util.ArrayList;
  6. import java.util.Calendar;
  7. import java.util.Date;
  8. import java.util.List;
  9. import java.util.Locale;
  10. import java.util.TimeZone;
  11. /**
  12. * Created by lzy on 2016/11/23.
  13. */
  14. public class DateUtils {
  15. private static String mYear; // 当前年
  16. private static String mMonth; // 月
  17. private static String mDay;//日
  18. /**
  19. * 获取当前日期几月几号
  20. */
  21. public static String getDateString() {
  22. final Calendar c = Calendar.getInstance();
  23. c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
  24. mYear = String.valueOf(c.get(Calendar.YEAR));// 获取当前年份
  25. mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 获取当前月份
  26. mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH));// 获取当前月份的日期号码
  27. if (Integer.parseInt(mDay) > MaxDayFromDay_OF_MONTH(Integer.parseInt(mYear), (Integer.parseInt(mMonth)))) {
  28. mDay = String.valueOf(MaxDayFromDay_OF_MONTH(Integer.parseInt(mYear), (Integer.parseInt(mMonth))));
  29. }
  30. return (mMonth.length() == 1 ? "0" + mMonth : mMonth) + "月" + (mDay.length() == 1 ? "0" + mDay : mDay) + "日";
  31. }
  32. /**
  33. * 获取当前年月日
  34. *
  35. * @return
  36. */
  37. public static String StringData() {
  38. final Calendar c = Calendar.getInstance();
  39. c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
  40. mYear = String.valueOf(c.get(Calendar.YEAR));// 获取当前年份
  41. mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 获取当前月份
  42. mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH));// 获取当前月份的日期号码
  43. if (Integer.parseInt(mDay) > MaxDayFromDay_OF_MONTH(Integer.parseInt(mYear), (Integer.parseInt(mMonth)))) {
  44. mDay = String.valueOf(MaxDayFromDay_OF_MONTH(Integer.parseInt(mYear), (Integer.parseInt(mMonth))));
  45. }
  46. return mYear + "-" + (mMonth.length() == 1 ? "0" + mMonth : mMonth) + "-" + (mDay.length() == 1 ? "0" + mDay : mDay);
  47. }
  48. /**
  49. * 根据当前日期获得是星期几
  50. *
  51. * @return
  52. */
  53. public static String getWeek(String time) {
  54. String Week = "";
  55. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  56. Calendar c = Calendar.getInstance();
  57. try {
  58. c.setTime(format.parse(time));
  59. } catch (ParseException e) {
  60. e.printStackTrace();
  61. }
  62. if (c.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
  63. Week += "周天";
  64. }
  65. if (c.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY) {
  66. Week += "周一";
  67. }
  68. if (c.get(Calendar.DAY_OF_WEEK) == Calendar.TUESDAY) {
  69. Week += "周二";
  70. }
  71. if (c.get(Calendar.DAY_OF_WEEK) == Calendar.WEDNESDAY) {
  72. Week += "周三";
  73. }
  74. if (c.get(Calendar.DAY_OF_WEEK) == Calendar.THURSDAY) {
  75. Week += "周四";
  76. }
  77. if (c.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY) {
  78. Week += "周五";
  79. }
  80. if (c.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) {
  81. Week += "周六";
  82. }
  83. return Week;
  84. }
  85. /**
  86. * 获取今天往后一周的日期(几月几号)
  87. */
  88. public static List<String> getSevendate() {
  89. List<String> dates = new ArrayList<String>();
  90. final Calendar c = Calendar.getInstance();
  91. c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
  92. for (int i = 0; i < 7; i++) {
  93. mYear = String.valueOf(c.get(Calendar.YEAR));// 获取当前年份
  94. mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 获取当前月份
  95. mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH) + i);// 获取当前日份的日期号码
  96. if (Integer.parseInt(mDay) > MaxDayFromDay_OF_MONTH(Integer.parseInt(mYear), (i + 1))) {
  97. mDay = String.valueOf(MaxDayFromDay_OF_MONTH(Integer.parseInt(mYear), (i + 1)));
  98. }
  99. String date = mMonth + "月" + mDay + "日";
  100. dates.add(date);
  101. }
  102. return dates;
  103. }
  104. /**
  105. * 获取今天往后一周的日期(哪年哪月哪日)
  106. */
  107. public static List<String> getSevendate2() {
  108. List<String> dates = new ArrayList<String>();
  109. final Calendar c = Calendar.getInstance();
  110. c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
  111. for (int i = 0; i < 7; i++) {
  112. mYear = String.valueOf(c.get(Calendar.YEAR));// 获取当前年份
  113. mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 获取当前月份
  114. mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH) + i);// 获取当前日份的日期号码
  115. if (Integer.parseInt(mDay) > MaxDayFromDay_OF_MONTH(Integer.parseInt(mYear), (i + 1))) {
  116. mDay = String.valueOf(MaxDayFromDay_OF_MONTH(Integer.parseInt(mYear), (i + 1)));
  117. }
  118. String date = mYear + "-" + (mMonth.length() == 1 ? "0" + mMonth : mMonth) + "-" + (mDay.length() == 1 ? "0" + mDay : mDay);
  119. dates.add(date);
  120. }
  121. return dates;
  122. }
  123. /**
  124. * 获取今天往后一周的日期(几号)
  125. */
  126. public static List<String> getSevenDay() {
  127. List<String> dates = new ArrayList<String>();
  128. final Calendar c = Calendar.getInstance();
  129. c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
  130. for (int i = 0; i < 7; i++) {
  131. mYear = String.valueOf(c.get(Calendar.YEAR));// 获取当前年份
  132. mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 获取当前月份
  133. mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH) + i);// 获取当前日份的日期号码
  134. if (Integer.parseInt(mDay) > MaxDayFromDay_OF_MONTH(Integer.parseInt(mYear), (i + 1))) {
  135. mDay = String.valueOf(MaxDayFromDay_OF_MONTH(Integer.parseInt(mYear), (i + 1)));
  136. }
  137. String date = mDay;
  138. dates.add(date);
  139. }
  140. return dates;
  141. }
  142. /**
  143. * 获取今天往后一周的日期(几月几号)
  144. */
  145. public static List<String> getSevenDayMonth() {
  146. List<String> dates = new ArrayList<String>();
  147. final Calendar c = Calendar.getInstance();
  148. c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
  149. for (int i = 0; i < 7; i++) {
  150. mYear = String.valueOf(c.get(Calendar.YEAR));// 获取当前年份
  151. mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 获取当前月份
  152. mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH) + i);// 获取当前日份的日期号码
  153. if (Integer.parseInt(mDay) > MaxDayFromDay_OF_MONTH(Integer.parseInt(mYear), (i + 1))) {
  154. mDay = String.valueOf(MaxDayFromDay_OF_MONTH(Integer.parseInt(mYear), (i + 1)));
  155. }
  156. String date = (mMonth.length() == 1 ? "0" + mMonth : mMonth) + "-" + (mDay.length() == 1 ? "0" + mDay : mDay);
  157. dates.add(date);
  158. }
  159. return dates;
  160. }
  161. /**
  162. * 获取今天往后一周的集合(周几)
  163. */
  164. public static List<String> get7week() {
  165. String week = "";
  166. List<String> weeksList = new ArrayList<String>();
  167. List<String> dateList = get7date();
  168. for (String s : dateList) {
  169. if (s.equals(StringData())) {
  170. week = "今天";
  171. } else {
  172. week = getWeek(s);
  173. }
  174. weeksList.add(week);
  175. }
  176. return weeksList;
  177. }
  178. /**
  179. * 获取今天往后一周的日期(yyyy-MM-dd 年-月-日)
  180. */
  181. public static List<String> get7date() {
  182. List<String> dates = new ArrayList<String>();
  183. final Calendar c = Calendar.getInstance();
  184. c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
  185. SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd");
  186. String date = sim.format(c.getTime());
  187. dates.add(date);
  188. for (int i = 0; i < 6; i++) {
  189. c.add(Calendar.DAY_OF_MONTH, 1);
  190. date = sim.format(c.getTime());
  191. dates.add(date);
  192. }
  193. return dates;
  194. }
  195. /**
  196. * 得到当年当月的最大日期
  197. */
  198. public static int MaxDayFromDay_OF_MONTH(int year, int month) {
  199. Calendar time = Calendar.getInstance();
  200. time.clear();
  201. time.set(Calendar.YEAR, year);
  202. time.set(Calendar.MONTH, month - 1);//注意,Calendar对象默认一月为0
  203. int day = time.getActualMaximum(Calendar.DAY_OF_MONTH);//本月份的天数
  204. return day;
  205. }
  206. public DateUtils() {}
  207. private static final DateUtils dateUtils = new DateUtils();
  208. public static DateUtils getInstance() {
  209. return dateUtils;
  210. }
  211. /**
  212. * 把当前日期转换格式为yyyy-MM-dd
  213. */
  214. public String format(Date date) {
  215. String format = "yyyy-MM-dd";
  216. SimpleDateFormat fmt = new SimpleDateFormat(format);
  217. return fmt.format(date);
  218. }
  219. /**
  220. * 把当前日期转换为指定格式
  221. */
  222. private String format(Date date, String format) {
  223. SimpleDateFormat fmt = new SimpleDateFormat(format);
  224. return fmt.format(date);
  225. }
  226. /**
  227. * 获得当前日期(yyyy-MM-dd)
  228. */
  229. public String getToday() {
  230. String result = "";
  231. Date date = new Date();
  232. result = format(date);
  233. return result;
  234. }
  235. /**
  236. * 获得明天日期(yyyy-MM-dd)
  237. */
  238. public String getTomorrow() {
  239. SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
  240. Calendar c = Calendar.getInstance();
  241. c.add(Calendar.DAY_OF_MONTH, 1);
  242. String result = sf.format(c.getTime());
  243. return result;
  244. }
  245. /**
  246. * 获得当前时间(yyyy-MM-dd HH:mm:ss)
  247. */
  248. public String getTime() {
  249. String result = "";
  250. Date date = new Date();
  251. result = format(date, "yyyy-MM-dd HH:mm:ss");
  252. return result;
  253. }
  254. /**
  255. * 调此方法输入所要转换的时间输入例如("2014-06-14 16:09:00")返回时间戳
  256. */
  257. public static long dateToLong(String time) {
  258. SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.CHINA);
  259. Date date;
  260. long l = 0;
  261. try {
  262. date = sdr.parse(time);
  263. l = date.getTime();
  264. } catch (ParseException e) {
  265. e.printStackTrace();
  266. }
  267. return l;
  268. }
  269. /**
  270. * 调此方法输入所要转换的时间、格式,返回时间戳
  271. */
  272. public static String getTimestamp(String time, String type) {
  273. SimpleDateFormat sdr = new SimpleDateFormat(type, Locale.CHINA);
  274. Date date;
  275. String times = null;
  276. try {
  277. date = sdr.parse(time);
  278. long l = date.getTime();
  279. String stf = String.valueOf(l);
  280. times = stf.substring(0, 10);//截取0-10
  281. } catch (ParseException e) {
  282. e.printStackTrace();
  283. }
  284. return times;
  285. }
  286. /**
  287. * 调用此方法输入所要转换的时间戳输入例如(1402733340)输出("2014年06月14日16时09分00秒")
  288. */
  289. public String times(String time) {
  290. SimpleDateFormat sdr = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
  291. int i = Integer.parseInt(time);
  292. String times = sdr.format(new Date(i * 1000L));
  293. return times;
  294. }
  295. /**
  296. * 调用此方法输入所要转换的时间戳输入例如(1402733340)输出("2014-06-14")
  297. */
  298. public static String times2(String time) {
  299. SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd");
  300. int i = Integer.parseInt(time);
  301. String times = sdr.format(new Date(i * 1000L));
  302. return times;
  303. }
  304. /**
  305. * 调用此方法输入所要转换的时间戳例如(1402733340)输出("06月14日 周六 16:09")
  306. */
  307. public static String times3(long timeStamp) {
  308. SimpleDateFormat sdr = new SimpleDateFormat("MM月dd日 # HH:mm");
  309. return sdr.format(new Date(timeStamp * 1000L)).replaceAll("#",
  310. getWeek(timeStamp));
  311. }
  312. /**
  313. * 输入时间戳变星期,例如(1402733340)输出("周六")
  314. */
  315. public static String getWeek(long timeStamp) {
  316. int mydate = 0;
  317. String week = null;
  318. Calendar cd = Calendar.getInstance();
  319. cd.setTime(new Date(timeStamp * 1000L));
  320. mydate = cd.get(Calendar.DAY_OF_WEEK);
  321. // 获取指定日期转换成星期几
  322. if (mydate == 1) {
  323. week = "周日";
  324. } else if (mydate == 2) {
  325. week = "周一";
  326. } else if (mydate == 3) {
  327. week = "周二";
  328. } else if (mydate == 4) {
  329. week = "周三";
  330. } else if (mydate == 5) {
  331. week = "周四";
  332. } else if (mydate == 6) {
  333. week = "周五";
  334. } else if (mydate == 7) {
  335. week = "周六";
  336. }
  337. return week;
  338. }
  339. /**
  340. * 通过年份和月份 得到当月的日子
  341. */
  342. public static int getMonthDays(int year, int month) {
  343. month++;
  344. switch (month) {
  345. case 1:
  346. case 3:
  347. case 5:
  348. case 7:
  349. case 8:
  350. case 10:
  351. case 12:
  352. return 31;
  353. case 4:
  354. case 6:
  355. case 9:
  356. case 11:
  357. return 30;
  358. case 2:
  359. if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
  360. return 29;
  361. } else {
  362. return 28;
  363. }
  364. default:
  365. return -1;
  366. }
  367. }
  368. /**
  369. * 根据列名 获取周
  370. */
  371. public static String getWeekName(int column) {
  372. switch (column) {
  373. case 0:
  374. return "周日";
  375. case 1:
  376. return "周一";
  377. case 2:
  378. return "周二";
  379. case 3:
  380. return "周三";
  381. case 4:
  382. return "周四";
  383. case 5:
  384. return "周五";
  385. case 6:
  386. return "周六";
  387. default:
  388. return "";
  389. }
  390. }
  391. private static ThreadLocal<SimpleDateFormat> DateLocal = new ThreadLocal<SimpleDateFormat>();
  392. /**
  393. * 判断是否为今天(效率比较高)
  394. *
  395. * @param day 传入的 时间 "2016-06-28 10:10:30"
  396. * @return true今天 false不是
  397. * @throws ParseException
  398. */
  399. public static boolean IsToday(String day) {
  400. try {
  401. Calendar pre = Calendar.getInstance();
  402. Date predate = new Date(System.currentTimeMillis());
  403. pre.setTime(predate);
  404. Calendar cal = Calendar.getInstance();
  405. Date date = getDateFormat().parse(day);
  406. cal.setTime(date);
  407. if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
  408. int diffDay = cal.get(Calendar.DAY_OF_YEAR) - pre.get(Calendar.DAY_OF_YEAR);
  409. if (diffDay == 0) {
  410. return true;
  411. }
  412. }
  413. } catch (ParseException e) {
  414. e.printStackTrace();
  415. }
  416. return false;
  417. }
  418. public static SimpleDateFormat getDateFormat() {
  419. if (null == DateLocal.get()) {
  420. DateLocal.set(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.CHINA));
  421. }
  422. return DateLocal.get();
  423. }
  424. /**
  425. * 判断给定字符串时间是否为今日(效率不是很高,不过也是一种方法)
  426. * 传入的 时间 "2016-06-28 10:10:30"
  427. * true今天 false不是
  428. */
  429. public static boolean isToday(String sdate) {
  430. boolean b = false;
  431. Date time = toDate(sdate);
  432. Date today = new Date();
  433. if (time != null) {
  434. String nowDate = dateFormater2.get().format(today);
  435. String timeDate = dateFormater2.get().format(time);
  436. if (nowDate.equals(timeDate)) {
  437. b = true;
  438. }
  439. }
  440. return b;
  441. }
  442. /**
  443. * 将字符串转位日期类型
  444. */
  445. public static Date toDate(String sdate) {
  446. try {
  447. return dateFormater.get().parse(sdate);
  448. } catch (ParseException e) {
  449. return null;
  450. }
  451. }
  452. private final static ThreadLocal<SimpleDateFormat> dateFormater = new ThreadLocal<SimpleDateFormat>() {
  453. @Override
  454. protected SimpleDateFormat initialValue() {
  455. return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  456. }
  457. };
  458. private final static ThreadLocal<SimpleDateFormat> dateFormater2 = new ThreadLocal<SimpleDateFormat>() {
  459. @Override
  460. protected SimpleDateFormat initialValue() {
  461. return new SimpleDateFormat("yyyy-MM-dd");
  462. }
  463. };
  464. }
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
电子表头数据读取并转换
Integer.parseInt用法integer.valueof的区别补充
java中String类型转换方法
JAVA string int Integer间的转换以及日期问题
java 进制转换
java基本数据类型转换
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服