java 日期格式化 字符串<->日期

2 篇文章 0 订阅
2 篇文章 0 订阅

日期格式多种多样,代码实现常用的数种转换

/**
 * 
 */
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;


/**
 * @author zhuce
 *
 */
public class PaiChu {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("start");

dateFormat("2017/1/10  19:00");

System.out.println("end");
}

public static String dateFormat(final String dateString) {
String string=dateString.replace("/", "-");
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date date = simpleDateFormat.parse(string);
string = simpleDateFormat.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}

simpleDateFormat = new SimpleDateFormat("yyyy-M-dd HH:mm");
try {
Date date = simpleDateFormat.parse(string);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
string = simpleDateFormat2.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}


simpleDateFormat = new SimpleDateFormat("yyyy-M-dd HH:mm:ss");
try {
Date date = simpleDateFormat.parse(string);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
string = simpleDateFormat2.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}
simpleDateFormat = new SimpleDateFormat("yyyy-MM-d H:m:s");
try {
Date date = simpleDateFormat.parse(string);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
string = simpleDateFormat2.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}


simpleDateFormat = new SimpleDateFormat("yyyy-MM-d HH:mm:ss");
try {
Date date = simpleDateFormat.parse(string);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
string = simpleDateFormat2.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}
simpleDateFormat = new SimpleDateFormat("yyyy-M-dd H:m:s");
try {
Date date = simpleDateFormat.parse(string);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
string = simpleDateFormat2.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}


simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd H:mm:ss");
try {
Date date = simpleDateFormat.parse(string);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
string = simpleDateFormat2.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}
simpleDateFormat = new SimpleDateFormat("yyyy-M-d HH:m:s");
try {
Date date = simpleDateFormat.parse(string);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
string = simpleDateFormat2.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}


simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:m:ss");
try {
Date date = simpleDateFormat.parse(string);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
string = simpleDateFormat2.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}
simpleDateFormat = new SimpleDateFormat("yyyy-M-d H:mm:s");
try {
Date date = simpleDateFormat.parse(string);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
string = simpleDateFormat2.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}


simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:s");
try {
Date date = simpleDateFormat.parse(string);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
string = simpleDateFormat2.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}
simpleDateFormat = new SimpleDateFormat("yyyy-M-d H:m:ss");
try {
Date date = simpleDateFormat.parse(string);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
string = simpleDateFormat2.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}


simpleDateFormat = new SimpleDateFormat("yyyy-M-d HH:mm:ss");
try {
Date date = simpleDateFormat.parse(string);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
string = simpleDateFormat2.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}
simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd H:m:s");
try {
Date date = simpleDateFormat.parse(string);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
string = simpleDateFormat2.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}


simpleDateFormat = new SimpleDateFormat("yyyy-M-dd H:mm:ss");
try {
Date date = simpleDateFormat.parse(string);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
string = simpleDateFormat2.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}
simpleDateFormat = new SimpleDateFormat("yyyy-MM-d HH:m:s");
try {
Date date = simpleDateFormat.parse(string);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
string = simpleDateFormat2.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}


simpleDateFormat = new SimpleDateFormat("yyyy-M-dd HH:m:ss");
try {
Date date = simpleDateFormat.parse(string);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
string = simpleDateFormat2.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}
simpleDateFormat = new SimpleDateFormat("yyyy-MM-d H:mm:s");
try {
Date date = simpleDateFormat.parse(string);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
string = simpleDateFormat2.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}


simpleDateFormat = new SimpleDateFormat("yyyy-M-dd HH:mm:s");
try {
Date date = simpleDateFormat.parse(string);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
string = simpleDateFormat2.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}
simpleDateFormat = new SimpleDateFormat("yyyy-MM-d H:m:ss");
try {
Date date = simpleDateFormat.parse(string);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
string = simpleDateFormat2.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}


simpleDateFormat = new SimpleDateFormat("yyyy-MM-d H:mm:ss");
try {
Date date = simpleDateFormat.parse(string);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
string = simpleDateFormat2.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}
simpleDateFormat = new SimpleDateFormat("yyyy-M-dd HH:m:s");
try {
Date date = simpleDateFormat.parse(string);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
string = simpleDateFormat2.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}


simpleDateFormat = new SimpleDateFormat("yyyy-MM-d HH:m:ss");
try {
Date date = simpleDateFormat.parse(string);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
string = simpleDateFormat2.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}
simpleDateFormat = new SimpleDateFormat("yyyy-M-dd H:mm:s");
try {
Date date = simpleDateFormat.parse(string);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
string = simpleDateFormat2.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}

simpleDateFormat = new SimpleDateFormat("yyyy-MM-d HH:mm:s");
try {
Date date = simpleDateFormat.parse(string);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
string = simpleDateFormat2.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}
simpleDateFormat = new SimpleDateFormat("yyyy-M-dd H:m:ss");
try {
Date date = simpleDateFormat.parse(string);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
string = simpleDateFormat2.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}


simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd H:m:ss");
try {
Date date = simpleDateFormat.parse(string);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
string = simpleDateFormat2.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}
simpleDateFormat = new SimpleDateFormat("yyyy-M-d HH:mm:s");
try {
Date date = simpleDateFormat.parse(string);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
string = simpleDateFormat2.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}


simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd H:mm:s");
try {
Date date = simpleDateFormat.parse(string);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
string = simpleDateFormat2.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}
simpleDateFormat = new SimpleDateFormat("yyyy-M-d HH:m:ss");
try {
Date date = simpleDateFormat.parse(string);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
string = simpleDateFormat2.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}


simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:m:s");
try {
Date date = simpleDateFormat.parse(string);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
string = simpleDateFormat2.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}
simpleDateFormat = new SimpleDateFormat("yyyy-M-d H:mm:ss");
try {
Date date = simpleDateFormat.parse(string);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
string = simpleDateFormat2.format(date);
return string;
} catch (Exception e) {
// TODO: handle exception
}


System.out.println(dateString + " line=" + Thread.currentThread().getStackTrace()[1].getLineNumber());
return string;
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值