java中的Date怎么转换成YYYYMMDD形式的?

hzhou 2005-12-31 07:11:45
我用
Date dt=new Date();
String s=DateFormat.getDateInstance(DateFormat.DEFAULT).format(dt).replaceAll("-","");
转换出来的有问题,比如2005年12月2日,它显示2005122,我其实想要20051202.
...全文
4090 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
datalover 2006-04-03
  • 打赏
  • 举报
回复
HFGH
gongdath 2005-12-31
  • 打赏
  • 举报
回复
SimpleDateFormat oDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
再用oDateFormat.format()方法。
chg2008 2005-12-31
  • 打赏
  • 举报
回复
DateFormat sdf = new SimpleDateFormat("yyyyMMdd",Locale.CHINA);
treeroot 2005-12-31
  • 打赏
  • 举报
回复
一个简单的问题不要搞复杂
weinickli 2005-12-31
  • 打赏
  • 举报
回复
Class SimpleDateFormat
bjbr 2005-12-31
  • 打赏
  • 举报
回复
做个标记
shazi_pig 2005-12-31
  • 打赏
  • 举报
回复
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

System.out.println(sdf.format(new Date()));
Mark_Chen 2005-12-31
  • 打赏
  • 举报
回复
二楼的厉害啊
把所有的日期格式都给出来了
看到他给滴那么仔细,我也就不用说了。嘿嘿
chw8219 2005-12-31
  • 打赏
  • 举报
回复
java.sql.Date date = new java.sql.Date( new java.util.Date().getTime())

这个date就是你要的那种格式了!
funcreal 2005-12-31
  • 打赏
  • 举报
回复
二楼正解
j2me_home 2005-12-31
  • 打赏
  • 举报
回复
public class DateUtilTools {

/**
*
* 方法名:
* 功能描述: 两个参数格式必须为20050827,而且不能为空。
* 参数说明: 第一个参数小于第二个参数返回true
* 返回值: // 函数返回值的说明
* 其他: // 其它说明
*/
public static boolean lessThan(String preDate, String date) {
if(preDate == null && date == null){
return false;
}
else if(preDate == null){
return true;
}
else if(date == null){
return false;
}
preDate = preDate.trim();
date = date.trim();
if(preDate.length() == 8 && date.length() == 8){
Integer date1 = new Integer(preDate);
Integer date2 = new Integer(date);
if(date1.compareTo(date2) < 0){
return true;
}
}
return false;
}


/**
*
* 方法名:
* 功能描述: 两个参数格式必须为20050827,而且不能为空。
* 参数说明: 第一个参数大于第二个参数返回true
* 返回值: // 函数返回值的说明
* 其他: // 其它说明
*/
public static boolean greaterThan(String preDate, String date) {
if(preDate == null && date == null){
return false;
}
else if(preDate == null){
return false;
}
else if(date == null){
return true;
}
preDate = preDate.trim();
date = date.trim();
if(preDate.length() == 8 && date.length() == 8){
Integer date1 = new Integer(preDate);
Integer date2 = new Integer(date);
if(date1.compareTo(date2) > 0){
return true;
}
}
return false;
}


/**
*
* 方法名:
* 功能描述: 两个参数格式必须为20050827,而且不能为空。
* 参数说明:
* 返回值: // 函数返回值的说明
* 其他: // 其它说明
*/
public static boolean equal(String preDate, String date) {
if(preDate == null && date == null){
return false;
}
else if(preDate == null){
return false;
}
else if(date == null){
return true;
}
preDate = preDate.trim();
date = date.trim();
if(preDate.length() == 8 && date.length() == 8){
Integer date1 = new Integer(preDate);
Integer date2 = new Integer(date);
if(date1.compareTo(date2) == 0){
return true;
}
}
return false;
}

/**
*
* @param 19位的时间 yyyy-MM-dd HH:mm:ss
*
* @return 15位的时间 yyyyMMdd HHmmss
*/
public static String _time19To15(String time_19) {
String time_15 = "";
if( time_19 == null || "".equals(time_19) || time_19.length() != 19 ) {
time_15 = "";
}else {
String[] r = time_19.replace('-','#').replace(':','#').split("#");
for(int i=0;i<r.length;i++)
{
time_15 += r[i];
}
}
return time_15;
}

/**
*
*
* @param 15位的时间 yyyyMMdd HHmmss
*
* @return 19位的时间 yyyy-MM-dd HH:mm:ss
*/
public static String _time15To19(String time_15) {
String time_19 = "";
if( time_15 == null || "".equals(time_15) || time_15.length() != 15 ) {
time_19 = "";
}else {
String y = time_15.substring(0, 4);
String m = time_15.substring(4, 6);
String d = time_15.substring(6, 8);
String h = time_15.substring(9, 11);
String mi = time_15.substring(11, 13);
String s = time_15.substring(13, 15);
time_19 = y+"-"+m+"-"+d+" "+h+":"+mi+":"+s;
}
return time_19;
}

/**
*
* @param 16位的时间 yyyy-MM-dd HH:mm
*
* @return 13位的时间 yyyyMMdd HHmm
*/
public static String _time16To13(String time_16) {
String time_13 = "";
if( time_16 == null || "".equals(time_16) || time_16.length() != 16 ) {
time_13 = "";
}else {
String[] r = time_16.replace('-','#').replace(':','#').split("#");
for(int i=0;i<r.length;i++)
{
time_13 += r[i];
}
}
return time_13;
}

/**
*
*
* @param 13位的时间 yyyyMMdd HHmm
*
* @return 16位的时间 yyyy-MM-dd HH:mm
*/
public static String _time13To16(String time_13) {
String time_16 = "";
if( time_13 == null || "".equals(time_13) || time_13.length() != 13 ) {
time_16 = "";
}else {
String y = time_13.substring(0, 4);
String m = time_13.substring(4, 6);
String d = time_13.substring(6, 8);
String h = time_13.substring(9, 11);
String mi = time_13.substring(11, 13);
time_16 = y+"-"+m+"-"+d+" "+h+":"+mi;
}
return time_16;
}

/**
*
*
* @param 10位的日期 yyyy-MM-dd
*
* @return 8位的日期 yyyyMMdd
*/
public static String _date10To8(String date_10) {
String date_8 = "";
if( date_10 == null || "".equals(date_10) || date_10.length() != 10 ) {
date_8 = "";
}else {
String[] r = date_10.split("-");
for(int i=0;i<r.length;i++)
{
date_8 += r[i];
}
}
return date_8;
}
/**
*
*
* @param 8位的日期 yyyyMMdd
*
* @return 10位的日期 yyyy-MM-dd
*/
public static String _date8To10(String date_8) {
String date_10 = "";
if( date_8 == null || "".equals(date_8) || date_8.length() != 8 ) {
date_10 = "";
}else {
String y = date_8.substring(0, 4);
String m = date_8.substring(4, 6);
String d = date_8.substring(6, 8);
date_10 = y+"-"+m+"-"+d;
}
return date_10;
}

/**
*
*
* @param 7位的日期 yyyy-MM
*
* @return 6位的日期 yyyyMM
*/
public static String _date7To6(String date_7) {
String date_6 = "";
if( date_7 == null || "".equals(date_7) || date_7.length() != 7 ) {
date_6 = "";
}else {
String[] r = date_7.split("-");
for(int i=0;i<r.length;i++)
{
date_6 += r[i];
}
}
return date_6;
}
/**
*
*
* @param 6位的日期 yyyyMM
*
* @return 7位的日期 yyyy-MM
*/
public static String _date6To7(String date_6) {
String date_7 = "";
if( date_6 == null || "".equals(date_6) || date_6.length() != 6 ) {
date_7 = "";
}else {
String y = date_6.substring(0, 4);
String m = date_6.substring(4, 6);
date_7 = y+"-"+m;
}
return date_7;
}
}
since2006 2005-12-31
  • 打赏
  • 举报
回复
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

System.out.println(sdf.format(new Date()));
yubokings 2005-12-31
  • 打赏
  • 举报
回复
假如你的格式是2005年12月2日
要转为20051202形式
你可以将
SimpleDateFormat dateformat1 = new SimpleDateFormat("yyyy年MM月dd日");
这样就可以将2005年12月2日字符串转化为日期型的了
yubokings 2005-12-31
  • 打赏
  • 举报
回复
SimpleDateFormat dateFormat1 = new SimpleDateFormat(yyyy-MM-dd);//设置解析格式
SimpleDateFormat dateFormat2 = new SimpleDateFormat(yyyyMMdd);//设置输出格式
Date date = dateFormat.parse("2005-12-01");
System.out.println(dateFormat2.format(date));
yubokings 2005-12-31
  • 打赏
  • 举报
回复
只要用SimpleDateFromat 设置一下日期输出格式就行了嘛。
SimpleDateFormat dateFormat = new SimpleDateFormat(yyyyMMdd);
Date date = dateFormat.parse("2005-12-01");
System.out.println(dateFormat.format(date));



import java.sql.Timestamp; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import org.apache.commons.lang.StringUtils; /** * 日付に関するユーティリティクラス * */ public class DateUtil { /** * 年月日時のフォーマット(年月日) */ public static final String YYYYMMDD = "yyyyMMdd"; /** * 年月日時のスラッシュ入りフォーマット(年/月/日) */ public static final String YYYYMMDD_SLASH = "yyyy/MM/dd"; /** * 年月のスラッシュ入りフォーマット(年/月) */ public static final String YYYYMM_SLASH = "yyyy/MM"; /** * 年月日時の間線入りフォーマット(年-月-日) */ public static final String YYYYMMDD_MID_LINE = "yyyy-MM-dd"; /** * 年月のスラッシュ入りフォーマット(年-月) */ public static final String YYYYMM_MID_LINE = "yyyy-MM"; /** * 年月日時分秒ミリ秒のフォーマット(年月日時分秒ミリ秒) */ public static final String YYYYMMDDHHMMSSSSS = "yyyyMMddHHmmssSSS"; /** * 年月日時分秒ミリ秒のフォーマット(年-月-日 時:分:秒) */ public static final String YYYYMMDDHHMMSS_MID_LINE = "yyyy-MM-dd HH:mm:ss"; /** * 年月日時分秒ミリ秒のフォーマット(年/月/日 時:分:秒) */ public static final String YYYYMMDDHHMMSS_SLASH = "yyyy/MM/dd HH:mm:ss"; /** * コンストラクタ */ private DateUtil() { // 何もしない } /** * システム日時を取得する * * @return システム日時 */ public static Date getSystemDate() { return new Date(); } /** * システム日時をTimestampオブジェクトで返却する * * @return Timestampオブジェクト */ public static Timestamp getTimestamp() { return toTimestamp(getSystemDate()); } /** * 指定するフォーマットのシステム日時を取得する * (2008年3月22日15時30分 → 2008-03-22 (format = "yyyy-MM-dd")) * @param format * 指定するフォーマット * @return システム日時 */ public static String getCurrentTime(String format) { return formatDateToStr(getSystemDate(), format); } /** * 現在の月を取得する * * @return 現在の月 */ public static String getCurentMonth() { SimpleDateFormat format = new SimpleDateFormat("MM"); return format.format(getSystemDate()); } /** * Nヶ月後の当月の最初の日を取得する * * @param n * Nヶ月後 * @return Nヶ月後の当月の最初の日 */ public static Calendar getMonthFirstDay(int n) { Calendar cal = Calendar.getInstance(); cal.set(Calendar.DAY_OF_MONTH, 1); cal.add(Calendar.MONTH, n); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); return cal; } /** * Nヶ月後の当月の最後の日を取得する * * @param n * Nヶ月後 * @return Nヶ月後の当月の最後の日 */ public static Calendar getMonthlastDay(int n) { Calendar cal = Calendar.getInstance(); cal.set(Calendar.DAY_OF_MONTH, 1); cal.add(Calendar.MONTH, n + 1); cal.add(Calendar.DATE, -1); cal.set(Calendar.HOUR_OF_DAY, 23); cal.set(Calendar.MINUTE, 59); cal.set(Calendar.SECOND, 59); return cal; } /** * 获得与某日期相隔几天的日期 * * @param date * 指定する日付 * @param addCount * 離れた日数 * @return 処理後の日付 */ public static Date addDay(Date date, int addCount) { if (date == null) { return null; } Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.DATE, addCount); return calendar.getTime(); } /** * 获得与某日期相隔几天的日期 * * @param date * 指定する日付 * @param addCount * 離れた月数 * @return 処理後の日付 */ public static Date addMonth(Date date, int addCount) { if (date == null) { return null; } Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.MONTH, addCount); return calendar.getTime(); } /** * DateオブジェクトをTimestampオブジェクトに変換する。 * * @param date * Dateオブジェクト * @return Timestamp オブジェクト */ public static Timestamp toTimestamp(Date date) { if (date == null) { return null; } return new Timestamp(date.getTime()); } /** * Dateオブジェクトを指定の形式でフォーマットする。 * 例:'yyyy年M月d日 h時m分' * @param date * Dateオブジェクト * @param dateFormat * Dateフォーマット * @return フォーマットされた文字列 */ public static String formatDateToStr(Date date, String dateFormat) { if (date == null) { return ""; } return getFormatter(dateFormat).format(date); } /** * 将某个日期格式字符串转换成另一指定格式日期字符串 例:传入"2009/1/15 16:58:00","yyyy/MM/dd * hh:mm:ss","yyyy年M月d日 a h时m分s秒" 则返回 “2009年1月15日 下午 4时58分0秒” * * @param dateStr * 指定する日付 * @param inDateFormat * 传入日期字符串的日期格式 * @param outDateFormat * 传出日期字符串的日期格式 * @return 返回指定格式的日期字符串 */ public static String formatDateToStr(String dateStr, String inDateFormat, String outDateFormat) { SimpleDateFormat simpleDateFormat = null; String str = ""; Date date = null; try { simpleDateFormat = getFormatter(inDateFormat); date = simpleDateFormat.parse(dateStr); simpleDateFormat.applyPattern(outDateFormat); return simpleDateFormat.format(date); } catch (Exception e) { return str; } } /** * 将字符串转化为格式为formmat 的日期格式返回 * * @param date * 待转换的日期 * @param formmat * 转换的format * @return 转换后的日期 */ public static Date stringToFormatDate(String date, String formmat) { try{ return getFormatter(formmat).parse(date); }catch(Exception e){ return null; } } /** * 日付の正確性チェック * * @param * dateStr 日期パラメータ * @param * format 日期フォーマット * @return 正しい:true 違い:false */ public static boolean isValidDate (String dateStr, String format) { try { getFormatter(format).parse(dateStr); return true; } catch (Exception e) { return false; } } /** * タイムの有効性チェック * * @param dateStr * タイム * @return 有効:true 無効:false */ public static boolean isValidTime (String dateStr) { boolean reval = false; if (!StringUtils.isBlank(dateStr)) { if (dateStr.length() == 4) { try { Integer.parseInt(dateStr); int hour = Integer.parseInt(dateStr.substring(0, 2)); int min = Integer.parseInt(dateStr.substring(2, 4)); if (hour <= 23 && hour >= 0 && min <= 59 && min >= 0) { return true; } } catch (Exception e) { } } } return reval; } /** * 根据输入的日期计算年龄 * * @param birthday 出生日期 格式:yyyy-MM-dd * @return String 年龄(周岁年龄) * */ public static String getAge(String birthday){ if (StringUtils.isBlank(birthday)) { return "0"; } Calendar birthdays = Calendar.getInstance(); try { birthdays.setTime(getFormatter(YYYYMMDD_MID_LINE).parse(birthday)); } catch (ParseException e) { return "0"; } Calendar today = new GregorianCalendar(); int age = today.get(Calendar.YEAR) - birthdays.get(Calendar.YEAR); birthdays.add(Calendar.YEAR, age); if (today.before(birthdays)) { age--; } return String.valueOf(age); } /** * 二つの日付の差を計算する *@param date1 計算日付1 *@param date2 計算日付2 * @return int 日付の差 */ public static int getDateDiffDays(Date date1 ,Date date2) { int retValue = 0; if (date1 != null && date2 != null) { long dateDiff = date1.getTime() - date2.getTime(); retValue = (int)(dateDiff/(24 * 60 * 60 * 1000)); } return retValue; } /** * 得到具体某个月的天数 * @throws ParseException */ public static int getDaysOfMonth(String year, String month) throws ParseException{ String datestr = year + "/" + month; Calendar cal = Calendar.getInstance(); SimpleDateFormat dateFormat = getFormatter(YYYYMM_SLASH); cal.setTime(dateFormat.parse(datestr)); int daysOfMonth = cal.getActualMaximum(Calendar.DAY_OF_MONTH); return daysOfMonth; } /** * 获取一个简单的日期格式化对象 * @param format * @return */ private static SimpleDateFormat getFormatter(String format) { return new SimpleDateFormat(format); } }

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧