public static void main(String[] args) { Date d1 = new Date(); Date d2 = new Date(System.currentTimeMillis()+10000); System.out.println("d1:"+d1+";d2:"+d2); System.out.println(d2.before(d1));//是否在其之前 System.out.println(d1.compareTo(d2));// }输出: d1:Tue Mar 22 13:47:47 CST 2016;d2:Tue Mar 22 13:47:57 CST 2016
public static String generateTimeCode() { Long time = new Date().getTime()/1000L; return Long.toHexString(time); }
public static void main(String[] args) { Date d1 = new Date(); System.out.println("d1:" + d1); Calendar calendar = Calendar.getInstance(); Date date = calendar.getTime(); Calendar calendar2 = Calendar.getInstance(); calendar2.setTime(date); System.out.println("date:" + date); System.out.println("calendar:" + calendar); System.out.println("calendar2:" + calendar2); }
public static void main(String[] args) { Calendar calendar = Calendar.getInstance(); System.out.println(calendar.get(YEAR)); System.out.println(calendar.get(MONTH)); System.out.println(calendar.get(DATE)); calendar.set(2016, 04, 04); System.out.println(calendar.getTime()); }输出:
/** * 功能描述:获取当前日期时间格式化字符串 * * @param dateFormat * 日期时间格式 */ public static String getCurrentDateTime(String dateFormat) { return new SimpleDateFormat(dateFormat).format(Calendar.getInstance() .getTime()); }
public static String getOffsetDate(String startTime, int field, int offset) { try { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Calendar calendar = Calendar.getInstance(); calendar.setTime(sdf.parse(startTime)); calendar.roll(field, offset); String offsetTime = sdf.format(calendar.getTime()); return offsetTime; } catch (Exception e) { e.printStackTrace(); } return null; }
Date and Time Patterns:
//IConstant.java public interface IConstant { public final static String DEFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss"; public final static String SHORT_DATE_FORMAT = "yyyy-MM-dd"; }
SimpleDateFormat sdf = new SimpleDateFormat(IConstant.DEFAULT_DATE_FORMAT); registerAddPO.setStartTime(sdf.format(new Date()));
public static void main(String[] args) { SimpleDateFormat sdf = new SimpleDateFormat(IConstant.DEFAULT_DATE_FORMAT); System.out.println("The Time is :\n"+sdf.format(new Date())); }
public static void main(String[] args) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd G"); System.out.println(sdf.format(new Date())); String str = "2016###三月###21"; SimpleDateFormat sdf1 = new SimpleDateFormat("y###MMM###d"); System.out.println(sdf1.parse(str)); }//输出 2016.03.22 公元