SimpleDateFormat f1= new SimpleDateFormat(); //其中没有些格式化参数,我们使用默认的日期格式。 System.out.println(f.formate(new Date())); 代码输出的日期格式为:12-3-22 下午4:36
SimpleDateFormat f4= new SimpleDateFormat("今天是"+"yyyy年MM月dd日 E kk点mm分");//可根据不同样式请求显示不同日期格式,要显示星期可以添加E参数 System.out.println(f4.format(new Date())); 代码输出的日期格式为:今天是2012年03月22日 星期四 16点46分
SimpleDateFormat formater = new SimpleDateFormat("yyyyMMdd hh:mm:ss"); System.out.println("Date to String "+formater.format(new Date())); 相近的常用形式还有 yyMMdd hh:mm:ss yyyy-MM-dd hh:mm:ss dd-MM-yyyy hh:mm:ss
应有的时候通常还会需要把具体日期转换为毫秒或者Timestamp形式,如下:
文本 - > Timestamp,日期 -> Timestamp Timestamp t ; SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); try ...{ t = new Timestamp(format.parse("2007-07-19 00:00:00").getTime()); } catch (ParseException e) ...{ e.printStackTrace(); } Timestamp t ; SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); t = new Timestamp(new Date().getTime());