**
1,创建当前时间 Date date = new Date(); 2,转换成String类型 String creatTime = sdf.format(date); 3,设置一个到期时间 String endtime= “2016-11-23”; 4,进行比较 DateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”); try { if(sdf.parse(nowtime).getTime()>sdf.parse(endtime).getTime()){//转成long类型比较 System.out.println(“当前时间大于到期时间”); }else if(sdf.parse(nowtime).getTime()<=sdf.parse(endtime).getTime()){ System.out.println(“当前时间小于等于到期时间”); } } catch (ParseException e) { e.printStackTrace(); }
1,转换成(yyyy/MM/dd)类型 DateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
DateFormat sd = new SimpleDateFormat(“yyyy/MM/dd”); String类型进行转换: String endtime= “2016-11-23 12:45:36”; sd.format(sdf.parse(endtime)); 2,获取不同类型的时间格式 public static String getCurrentTime(int type) { String currentTime = “”; Date date = new Date(); switch (type) { case 1: SimpleDateFormat sdf1 = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”); currentTime = sdf1.format(new Date()); break; case 2: SimpleDateFormat sdf2 = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss.SSS”); currentTime = sdf2.format(new Date()); break; case 3: SimpleDateFormat sdf3 = new SimpleDateFormat(“yyyyMMddHHmmss”); currentTime = sdf3.format(new Date()); break; case 4: SimpleDateFormat sdf4 = new SimpleDateFormat(“yyyy-MM-dd”); currentTime = sdf4.format(new Date()); break; case 5: //转换年月日 SimpleDateFormat sdf5 = new SimpleDateFormat(“yyyyMMdd”); currentTime = sdf5.format(date); break; case 6: //转换时分秒 SimpleDateFormat sdf6 = new SimpleDateFormat(“HHmmss”); currentTime = sdf6.format(date); break; default: SimpleDateFormat sdf = new SimpleDateFormat(“yyyyMMddHHmmss”); currentTime = sdf.format(new Date()); } return currentTime; } 3,获取当前月的前一个月、三个月、一年 public static String getStrTime(Date date,int type) { String currentTime = “”; SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”); //Date date = new Date(); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); switch (type) { case 1: calendar.add(Calendar.MONTH, -1);//当前时间前去一个月,即一个月前的时间 Date strDate =calendar.getTime();//获取一年前的时间,或者一个月前的时间 currentTime = sdf.format(strDate); break; case 2: calendar.add(Calendar.MONTH, -3);//当前时间前去三个月,即一个月前的时间 Date strDate3 =calendar.getTime();//获取一年前的时间,或者一个月前的时间 currentTime = sdf.format(strDate3); break; case 3: calendar.add(Calendar.MONTH, -6);//当前时间前去三个月,即一个月前的时间 Date strDate6 =calendar.getTime();//获取一年前的时间,或者一个月前的时间 currentTime = sdf.format(strDate6); break; case 4: calendar.add(Calendar.YEAR, -1);//当前时间减去一年,即一年前的时间 Date strDateYear =calendar.getTime();//获取一年前的时间,或者一个月前的时间 currentTime = sdf.format(strDateYear); break; default: SimpleDateFormat sdf7 = new SimpleDateFormat(“yyyyMMddHHmmss”); currentTime = sdf.format(new Date()); } return currentTime; } 4,时间格式转换(把字符串类型的 yyyyMMdd 类型转换成 yyyy-MM-dd) public static String getStrDate(String str) { String dataStr = “”; try { Date data = new SimpleDateFormat(“yyyyMMdd”).parse(str); dataStr = new SimpleDateFormat(“yyyy-MM-dd”).format(data); } catch (ParseException e) { e.printStackTrace(); } return dataStr; } 5,时间格式转换(把字符串类型的 yyyy-MM-dd 类型转换成 yyyyMMdd) public static String strDate(String str) { SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd”); SimpleDateFormat sdfer = new SimpleDateFormat(“yyyyMMdd”); String dataStr = “”; try { Date format = sdf.parse(str); dataStr = sdfer.format(format); } catch (ParseException e) { e.printStackTrace(); } return dataStr; } 6,时间格式转换(把字符串类型的 HH:mm:ss 类型转换成 HHmmss) public static String getStrTime(String str) { SimpleDateFormat sdf = new SimpleDateFormat(“HH:mm:ss”); SimpleDateFormat sdfer = new SimpleDateFormat(“HHmmss”); String dataStr = “”; try { Date format = sdf.parse(str); dataStr = sdfer.format(format); } catch (ParseException e) { e.printStackTrace(); } return dataStr; }