`
zhxw121
  • 浏览: 61971 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

由日期得出类似“20100520”的字符串

    博客分类:
  • J2EE
阅读更多
package com.basic.action;
/**
 *比如我们要做这样一件事,有个时间“2010-05-20 11:11:11”
 *我们要取得类似“20100520” 则可以用下面的代码来实现
 */
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Test {

	
	public static void main(String[] args) {
		
		Date date = new   Date(System.currentTimeMillis());//获得当前时间
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");//格式化转换 
		String str=sdf.format(date);//把日期转换为字符
		String re = "";
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		re = c.get(Calendar.YEAR)+""+((c.get(Calendar.MONTH)+1)<10?"0"+(c.get(Calendar.MONTH)+1):(c.get(Calendar.MONTH)+1))+""+
			 (c.get(Calendar.DAY_OF_MONTH)<10?"0"+c.get(Calendar.DAY_OF_MONTH):c.get(Calendar.DAY_OF_MONTH))  ;

		System.out.println(re);
	
	}

}

 

分享到:
评论
1 楼 sxlkk 2010-06-13  
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		System.out.println(sdf.format(new java.util.Date()));

  

相关推荐

Global site tag (gtag.js) - Google Analytics