首页 > 代码库 > Android 开发笔记___DateUtil——Time
Android 开发笔记___DateUtil——Time
1 package com.example.alimjan.hello_world; 2 3 /** 4 * Created by alimjan on 6/30/2017. 5 */ 6 7 import java.text.ParseException; 8 import java.text.SimpleDateFormat; 9 import java.util.Calendar; 10 import java.util.Date; 11 12 13 public class DateUtil { 14 15 private static final String FORMAT = "yyyy-MM-dd HH:mm:ss"; 16 17 public static Date str2Date(String str) { 18 return str2Date(str, null); 19 } 20 21 public static Date str2Date(String str, String format) { 22 if (str == null || str.length() == 0) { 23 return null; 24 } 25 if (format == null || format.length() == 0) { 26 format = FORMAT; 27 } 28 Date date = null; 29 try { 30 SimpleDateFormat sdf = new SimpleDateFormat(format); 31 date = sdf.parse(str); 32 33 } catch (Exception e) { 34 e.printStackTrace(); 35 } 36 return date; 37 38 } 39 40 public static Calendar str2Calendar(String str) { 41 return str2Calendar(str, null); 42 43 } 44 45 public static Calendar str2Calendar(String str, String format) { 46 47 Date date = str2Date(str, format); 48 if (date == null) { 49 return null; 50 } 51 Calendar c = Calendar.getInstance(); 52 c.setTime(date); 53 54 return c; 55 56 } 57 58 public static String date2Str(Calendar c) {// yyyy-MM-dd HH:mm:ss 59 return date2Str(c, null); 60 } 61 62 public static String date2Str(Calendar c, String format) { 63 if (c == null) { 64 return null; 65 } 66 return date2Str(c.getTime(), format); 67 } 68 69 public static String date2Str(Date d) {// yyyy-MM-dd HH:mm:ss 70 return date2Str(d, null); 71 } 72 73 public static String date2Str(Date d, String format) {// yyyy-MM-dd HH:mm:ss 74 if (d == null) { 75 return null; 76 } 77 if (format == null || format.length() == 0) { 78 format = FORMAT; 79 } 80 SimpleDateFormat sdf = new SimpleDateFormat(format); 81 String s = sdf.format(d); 82 return s; 83 } 84 85 public static String getCurDateStr() { 86 Calendar c = Calendar.getInstance(); 87 c.setTime(new Date()); 88 return c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH) + 1) + "-" 89 + c.get(Calendar.DAY_OF_MONTH) + "-" 90 + c.get(Calendar.HOUR_OF_DAY) + ":" + c.get(Calendar.MINUTE) 91 + ":" + c.get(Calendar.SECOND); 92 } 93 94 /** 95 * 获得当前日期的字符串格式 96 * 97 * @param format 98 * @return 99 */ 100 public static String getCurDateStr(String format) { 101 Calendar c = Calendar.getInstance(); 102 return date2Str(c, format); 103 } 104 105 // 格式到秒 106 public static String getMillon(long time) { 107 108 return new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(time); 109 110 } 111 112 // 格式到天 113 public static String getDay(long time) { 114 115 return new SimpleDateFormat("yyyy-MM-dd").format(time); 116 117 } 118 119 // 格式到毫秒 120 public static String getSMillon(long time) { 121 122 return new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss-SSS").format(time); 123 124 } 125 126 127 /* 128 输入的是String,格式诸如20120102,实现加一天的功能,返回的格式为String,诸如20120103 129 */ 130 public static String stringDatePlus(String row) throws ParseException{ 131 String year=row.substring(0, 4); 132 String month=row.substring(4,6); 133 String day=row.substring(6); 134 String date1=year+"-"+month+"-"+day; 135 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); 136 Date startDate=sdf.parse(date1); 137 Calendar cd = Calendar.getInstance(); 138 cd.setTime(startDate); 139 cd.add(Calendar.DATE, 1); 140 String dateStr =sdf.format(cd.getTime()); 141 String year1=dateStr.substring(0,4); 142 String month1=dateStr.substring(5,7); 143 String day1=dateStr.substring(8); 144 return year1+month1+day1; 145 } 146 147 /* 148 输入的是String,格式诸如20120102,实现减一天的功能,返回的格式为String,诸如20120101 149 */ 150 public static String stringDateDecrease(String row) throws ParseException{ 151 String year=row.substring(0, 4); 152 String month=row.substring(4,6); 153 String day=row.substring(6); 154 String date1=year+"-"+month+"-"+day; 155 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); 156 Date startDate=sdf.parse(date1); 157 Calendar cd = Calendar.getInstance(); 158 cd.setTime(startDate); 159 cd.add(Calendar.DATE, -1); 160 String dateStr =sdf.format(cd.getTime()); 161 String year1=dateStr.substring(0,4); 162 String month1=dateStr.substring(5,7); 163 String day1=dateStr.substring(8); 164 return year1+month1+day1; 165 } 166 167 /* 168 输入的格式为String,诸如20120101,返回的格式为String,诸如2012-01-01 169 */ 170 public static String stringDateChange(String date) 171 { 172 if(date.length()=="20120101".length()){ 173 String year=date.substring(0, 4); 174 String month=date.substring(4,6); 175 String day=date.substring(6); 176 return year+"-"+month+"-"+day; 177 }else{ 178 return date; 179 } 180 181 182 } 183 /** 184 * 日期向后推一天 185 * @param date 格式:20120101 186 * @return 20120102 187 */ 188 public static String tonextday(String date){ 189 int year = Integer.parseInt(date.substring(0,4)); 190 int month = Integer.parseInt(date.substring(4,6)); 191 int day = Integer.parseInt(date.substring(6)); 192 Calendar calendar = Calendar.getInstance(); 193 calendar.set(year, month-1, day+1); 194 Date newdate = calendar.getTime(); 195 SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd"); 196 String da = format.format(newdate); 197 return da; 198 } 199 200 /** 201 * 获取当前日期上一周的开始日期 (周日) 202 */ 203 public static String previousWeekByDate(String date) { 204 int year = Integer.parseInt(date.substring(0,4)); 205 int month = Integer.parseInt(date.substring(4,6)); 206 int day = Integer.parseInt(date.substring(6)); 207 Calendar calendar = Calendar.getInstance(); 208 calendar.set(year, month-1, day); 209 Date newdate = calendar.getTime(); 210 SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd"); 211 Calendar cal = Calendar.getInstance(); 212 cal.setTime(newdate); 213 int dayWeek = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天 214 if(1 == dayWeek) { 215 cal.add(Calendar.DAY_OF_MONTH, -1); 216 } 217 cal.setFirstDayOfWeek(Calendar.SUNDAY);//设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一 218 int s = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天 219 cal.add(Calendar.DATE, cal.getFirstDayOfWeek()-s);//根据日历的规则,给当前日期减往星期几与一个星期第一天的差值 220 cal.add(Calendar.WEEK_OF_YEAR, -1); 221 String imptimeBegin = sdf.format(cal.getTime()); 222 // System.out.println("所在周星期日的日期:"+imptimeBegin); 223 return imptimeBegin; 224 } 225 226 227 /** 228 * 获取当前日期上一周的结束日期 (周六) 229 */ 230 public static String previousWeekEndDayByDate(String date) { 231 int year = Integer.parseInt(date.substring(0,4)); 232 int month = Integer.parseInt(date.substring(4,6)); 233 int day = Integer.parseInt(date.substring(6)); 234 Calendar calendar = Calendar.getInstance(); 235 calendar.set(year, month-1, day); 236 Date newdate = calendar.getTime(); 237 SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd"); 238 Calendar cal = Calendar.getInstance(); 239 cal.setTime(newdate); 240 int dayWeek = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天 241 if(1 == dayWeek) { 242 cal.add(Calendar.DAY_OF_MONTH, -1); 243 } 244 cal.setFirstDayOfWeek(Calendar.SUNDAY);//设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一 245 int s = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天 246 cal.add(Calendar.DATE, cal.getFirstDayOfWeek()+(6-s)); 247 cal.add(Calendar.WEEK_OF_YEAR, -1); 248 String imptimeBegin = sdf.format(cal.getTime()); 249 // System.out.println("星期六的日期:"+imptimeBegin); 250 return imptimeBegin; 251 } 252 253 254 /** 255 * 获取当前日期当前一周的开始日期 (周日) 256 */ 257 public static String getCurrentWeekFirstDayByDate(String date) { 258 int year = Integer.parseInt(date.substring(0,4)); 259 int month = Integer.parseInt(date.substring(4,6)); 260 int day = Integer.parseInt(date.substring(6)); 261 Calendar calendar = Calendar.getInstance(); 262 calendar.set(year, month-1, day); 263 Date newdate = calendar.getTime(); 264 SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd"); 265 Calendar cal = Calendar.getInstance(); 266 cal.setTime(newdate); 267 int dayWeek = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天 268 if(1 == dayWeek) { 269 cal.add(Calendar.DAY_OF_MONTH, -1); 270 } 271 cal.setFirstDayOfWeek(Calendar.SUNDAY);//设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一 272 int s = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天 273 cal.add(Calendar.DATE, cal.getFirstDayOfWeek()-s);//根据日历的规则,给当前日期减往星期几与一个星期第一天的差值 274 275 String imptimeBegin = sdf.format(cal.getTime()); 276 // System.out.println("所在周星期日的日期:"+imptimeBegin); 277 return imptimeBegin; 278 } 279 /** 280 * 获取当前日期当前一周的结束日期 (周六) 281 */ 282 public static String getCurrentWeekEndDayByDate(String date) { 283 int year = Integer.parseInt(date.substring(0,4)); 284 int month = Integer.parseInt(date.substring(4,6)); 285 int day = Integer.parseInt(date.substring(6)); 286 Calendar calendar = Calendar.getInstance(); 287 calendar.set(year, month-1, day); 288 Date newdate = calendar.getTime(); 289 SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd"); 290 Calendar cal = Calendar.getInstance(); 291 cal.setTime(newdate); 292 int dayWeek = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天 293 if(1 == dayWeek) { 294 cal.add(Calendar.DAY_OF_MONTH, -1); 295 } 296 cal.setFirstDayOfWeek(Calendar.SUNDAY);//设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一 297 int s = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天 298 cal.add(Calendar.DATE, cal.getFirstDayOfWeek()+(6-s)); 299 300 String imptimeBegin = sdf.format(cal.getTime()); 301 return imptimeBegin; 302 } 303 304 305 /** 306 * 返回上一个月的第一天 307 * @param date20120304 308 * @return 20120201 309 */ 310 public static String previousMonthByDate(String date) { 311 // TODO Auto-generated method stub 312 int year = Integer.parseInt(date.substring(0,4)); 313 int month = Integer.parseInt(date.substring(4,6)); 314 int day = Integer.parseInt(date.substring(6)); 315 Calendar calendar = Calendar.getInstance(); 316 calendar.set(year, month-2, 1); 317 Date newdate = calendar.getTime(); 318 SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd"); 319 Calendar cal = Calendar.getInstance(); 320 cal.setTime(newdate); 321 String imptimeBegin = sdf.format(cal.getTime()); 322 // System.out.println(imptimeBegin); 323 return imptimeBegin; 324 } 325 326 /** 327 * 返回下一个月的第一天 328 * @param date20120304 329 * @return 20120401 330 */ 331 public static String nextMonthByDate(String date) { 332 // TODO Auto-generated method stub 333 int year = Integer.parseInt(date.substring(0,4)); 334 int month = Integer.parseInt(date.substring(4,6)); 335 int day = Integer.parseInt(date.substring(6)); 336 Calendar calendar = Calendar.getInstance(); 337 calendar.set(year, month, 1); 338 Date newdate = calendar.getTime(); 339 SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd"); 340 Calendar cal = Calendar.getInstance(); 341 cal.setTime(newdate); 342 String imptimeBegin = sdf.format(cal.getTime()); 343 // System.out.println(imptimeBegin); 344 return imptimeBegin; 345 } 346 /** 347 * 返回当前月的第一天 348 * @param date20120103 349 * @return 20120101 350 */ 351 public static String getCurrentMonthFirstDayByDate(String date) { 352 int year = Integer.parseInt(date.substring(0,4)); 353 int month = Integer.parseInt(date.substring(4,6)); 354 int day = Integer.parseInt(date.substring(6)); 355 Calendar calendar = Calendar.getInstance(); 356 calendar.set(year, month-1, 1); 357 Date newdate = calendar.getTime(); 358 SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd"); 359 Calendar cal = Calendar.getInstance(); 360 cal.setTime(newdate); 361 String imptimeBegin = sdf.format(cal.getTime()); 362 return imptimeBegin; 363 } 364 365 }
Android 开发笔记___DateUtil——Time
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。