查看: 2814|回复: 0

[手机开发] Android处理时间各种方法汇总

发表于 2017-12-25 08:00:00

本文实例为大家分享了Android处理时间的各种方法,供大家参考,具体内容如下

  1. /**
  2. * 获取当前时间
  3. *
  4. * @return 当前时间
  5. */
  6. public static String getdate() {
  7. SimpleDateFormat formatter = new SimpleDateFormat("HH:mm");
  8. Date curDate = new Date(System.currentTimeMillis());// 获取当前时间
  9. String time = formatter.format(curDate);
  10. return time;
  11. }
  12. /**
  13. * 获取当前秒数
  14. * @return
  15. */
  16. public static String getSecond(){
  17. Calendar calendar = Calendar.getInstance();
  18. int sec = calendar.get(Calendar.SECOND);
  19. String value = String.valueOf(sec<10?"0"+sec:sec);
  20. return value;
  21. }
  22. /**
  23. * 获取当天日期
  24. *
  25. * @return 当前时间
  26. */
  27. public static String getriqi() {
  28. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  29. Date curDate = new Date(System.currentTimeMillis());// 获取当天日期
  30. String time = formatter.format(curDate);
  31. return time;
  32. }
  33. /**
  34. * 获取当前全部时间
  35. *
  36. * @return 当前时间
  37. */
  38. public static String getalldate() {
  39. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  40. Date curDate = new Date(System.currentTimeMillis());// 获取当前时间
  41. String time = formatter.format(curDate);
  42. return time;
  43. }
  44. public static Date getDate(String time){
  45. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  46. Date date = null;
  47. try {
  48. date = formatter.parse(time);
  49. } catch (ParseException e) {
  50. e.printStackTrace();
  51. }
  52. return date;
  53. }
  54. /**
  55. * 天数减一
  56. */
  57. public static String Yesterday(int data) {
  58. SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
  59. Date beginDate = new Date();
  60. Calendar date = Calendar.getInstance();
  61. date.setTime(beginDate);
  62. date.set(Calendar.DATE, date.get(Calendar.DATE) - data);
  63. // Date endDate = dft.parse(dft.format(date.getTime()));
  64. String time = dft.format(date.getTime());
  65. return time;
  66. }
  67. /**
  68. * 获取星期
  69. *
  70. * @param date日期
  71. * ****-**-**
  72. * @return
  73. */
  74. public static int getDay(Date date) {
  75. Calendar cal = Calendar.getInstance();
  76. cal.setTime(date);
  77. return cal.get(Calendar.DAY_OF_WEEK);
  78. }
  79. /**
  80. * 时间顺序判断
  81. * @param data_ruke
  82. * @param data_choice
  83. * @return
  84. */
  85. public static boolean conpare_data(String data1,String data2){
  86. if(data1==null||"".equals(data1)){
  87. return true;
  88. }
  89. DateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  90. try{
  91. Date dt1 = dFormat.parse(data1);
  92. Date dt2 = dFormat.parse(data2);
  93. if(dt1.getTime()>dt2.getTime()){
  94. Log.i("time_compare", "dt1在dt2之后");
  95. return false;
  96. }else if (dt1.getTime() < dt2.getTime()) {
  97. Log.i("time_compare", "dt1在dt2之前");
  98. return true;
  99. }
  100. }catch(Exception e){
  101. e.printStackTrace();
  102. }
  103. return false;
  104. }
  105. /**
  106. * 日期比较
  107. */
  108. public static int compare_date(String DATE1, String DATE2) {
  109. DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
  110. try {
  111. Date dt1 = df.parse(DATE1);
  112. Date dt2 = df.parse(DATE2);
  113. if (dt1.getTime() > dt2.getTime()) {
  114. if (dt1.getTime() - dt2.getTime() == 86400000) {
  115. return 1;
  116. } else if (dt1.getTime() - dt2.getTime() == (86400000 * 2)) {
  117. return 2;
  118. } else if (dt1.getTime() - dt2.getTime() == (86400000 * 3)) {
  119. return 3;
  120. } else {
  121. return 0;
  122. }
  123. } else {
  124. return 0;
  125. }
  126. } catch (Exception exception) {
  127. exception.printStackTrace();
  128. return 0;
  129. }
  130. }
  131. /**
  132. * 返回两个string类型日期之间相差的天数
  133. */
  134. public static int daysBetween(String smdate,String bdate){
  135. SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
  136. Calendar cal = Calendar.getInstance();
  137. long time1 = 0;
  138. long time2 = 0;
  139. try{
  140. cal.setTime(sdf.parse(smdate));
  141. time1 = cal.getTimeInMillis();
  142. cal.setTime(sdf.parse(bdate));
  143. time2 = cal.getTimeInMillis();
  144. }catch(Exception e){
  145. e.printStackTrace();
  146. }
  147. long between_days=(time2-time1)/(1000*3600*24);
  148. return Integer.parseInt(String.valueOf(between_days));
  149. }
  150. /**
  151. * 返回两个string类型日期相差的小时数
  152. */
  153. public static int daysBetween2(String startTime, String endTime) {
  154. SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH");
  155. Calendar cal = Calendar.getInstance();
  156. long time1 = 0;
  157. long time2 = 0;
  158. try{
  159. cal.setTime(sdf.parse(startTime));
  160. time1 = cal.getTimeInMillis();
  161. cal.setTime(sdf.parse(endTime));
  162. time2 = cal.getTimeInMillis();
  163. }catch(Exception e){
  164. e.printStackTrace();
  165. }
  166. long between_days=(time2-time1)/(1000*3600);
  167. return Integer.parseInt(String.valueOf(between_days));
  168. }
  169. /**
  170. * 计算两段日期的重合日期
  171. */
  172. /**
  173. * 计算两段日期的重合日期
  174. * @param str1 开始日期1
  175. * @param str2 结束日期1
  176. * @param str3 开始日期2
  177. * @param str4 结束日期2
  178. * @return
  179. * @throws Exception
  180. */
  181. public static Map<String,Object> comparisonRQ(String str1, String str2, String str3,
  182. String str4) throws Exception {
  183. String mesg = "";
  184. DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
  185. String startdate = "";
  186. String enddate = "";
  187. try {
  188. Date dt1 = df.parse(str1);
  189. Date dt2 = df.parse(str2);
  190. Date dt3 = df.parse(str3);
  191. Date dt4 = df.parse(str4);
  192. if (dt1.getTime()<=dt3.getTime()&&dt3.getTime()<=dt2.getTime()&&dt2.getTime()<=dt4.getTime()) {
  193. mesg = "f";//重合
  194. startdate = str3;
  195. enddate = str2;
  196. }
  197. if (dt1.getTime()>=dt3.getTime()&&dt3.getTime()<=dt2.getTime()&&dt2.getTime()<=dt4.getTime()) {
  198. mesg = "f";//重合
  199. startdate = str1;
  200. enddate = str2;
  201. }
  202. if (dt3.getTime()<=dt1.getTime()&&dt1.getTime()<=dt4.getTime()&&dt4.getTime()<=dt2.getTime()) {
  203. mesg = "f";//重合
  204. startdate = str1;
  205. enddate = str4;
  206. }
  207. if (dt3.getTime()>=dt1.getTime()&&dt1.getTime()<=dt4.getTime()&&dt4.getTime()<=dt2.getTime()) {
  208. mesg = "f";//重合
  209. startdate = str3;
  210. enddate = str4;
  211. }
  212. System.out.println(startdate+"----"+enddate);
  213. }catch (ParseException e) {
  214. e.printStackTrace();
  215. throw new ParseException(e.getMessage(), 0);
  216. }catch(Exception e){
  217. e.printStackTrace();
  218. throw new Exception(e);
  219. }
  220. Map<String,Object> map = new HashMap<String,Object>();
  221. map.put("startdate", startdate);
  222. map.put("enddate", enddate);
  223. return map;
  224. }
复制代码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持程序员之家。



回复

使用道具 举报