Date / time formatting operation

Format and output the current date and time

		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); //指定日期格式
		SimpleDateFormat sdf1 = new SimpleDateFormat("HHmmss");  //指定时间格式
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd:HHmmss");  //指定日期、时间格式
		Calendar cal = Calendar.getInstance();            //获取当前日期、时间
		String curDate = null;
		String curtime = null;
		String date = null;
        
        date = sdf2.format(cal.getTime);  //格式化输入时间
		curtime = sdf1.format(cal.getTime());   //格式化输出时间
		curDate = sdf.format(cal.getTime());  //格式化输出日期
Get the time or date in a few days or hours
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		SimpleDateFormat sdf1 = new SimpleDateFormat("HHmmss");
		SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd:HHmmss");
		Calendar cal = Calendar.getInstance();
		String curDate = null;
		String curtime = null;
		String date = null;
		
		cal.add(Calendar.DATE, Integer.parseInt(validTime)); //当前几天后或几天前(例2或者-2)日期,参数2为对应天数
		cal.add(Calendar.HOUR, Integer.parseInt(validTime)); //几小时候或者几小时前,参数2为对应小时数
		curtime = sdf1.format(cal.getTime());  
		curDate = sdf.format(cal.getTime());
		date = sdf2.format(cal.getTime);
Get current timestamp

long timeFlag = System.currentTimeMillis();
Get the specified date time stamp

public static void main(String[] arg) throws ParseException{
     SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd");
     Date date=simpleDateFormat .parse("2010-06-25");
     int timeStemp = date.getTime();
     System.out.println(timeStemp );
}




Published 20 original articles · Likes6 · Visits 10,000+

Guess you like

Origin blog.csdn.net/weixin_36662608/article/details/71142590