日期/时间格式化操作

来源:互联网 发布:淘宝宝贝卖点大全 编辑:程序博客网 时间:2024/05/24 07:21

格式化输出当前日期、时间

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());  //格式化输出日期
获取几日后或几小时后的时间或日期
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);
获取当前时间戳

long timeFlag = System.currentTimeMillis();
获取指定日期时间戳

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 );}




0 0
原创粉丝点击