JAVA 利用SimpleDateFormat将String转换为格式化的日期
1.
/**
* 使用用户格式提取字符串日期
*
* @param strDate 日期字符串
* @param pattern 日期格式
* @return
*/
public static Date parse(String strDate, String pattern) {
SimpleDateFormat df = new SimpleDateFormat(pattern);
try {
return df.parse(strDate);
} catch (ParseException e) {
return null;
}
}
2.
/**
*
* 将20120324
* 解析为:2012-03-24
*
*/
private void parseTime1() {
try {
String time = "";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
// SimpleDateFormat的parse(String time)方法将String转换为Date
Date date = simpleDateFormat.parse(time);
simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
// SimpleDateFormat的format(Date date)方法将Date转换为String
String formattedTime = simpleDateFormat.format(date);
System.out.println("---->将" + time + "解析为:" + formattedTime);
} catch (Exception e) { }
}
3.
/***
*
* 将20131227085009
* 解析为:2013-12-27 08:50:09
*
**/
private void parseTime2() {
try {
String time = "";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
// SimpleDateFormat的parse(String time)方法将String转换为Date
Date date = simpleDateFormat.parse(time);
simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// SimpleDateFormat的format(Date date)方法将Date转换为String
String formattedTime = simpleDateFormat.format(date);
System.out.println("---->将" + time + "解析为:" + formattedTime);
} catch (Exception e) { }
}
JAVA 利用SimpleDateFormat将String转换为格式化的日期的更多相关文章
- 【转】深入理解Java:SimpleDateFormat安全的时间格式化
[转]深入理解Java:SimpleDateFormat安全的时间格式化 想必大家对SimpleDateFormat并不陌生.SimpleDateFormat 是 Java 中一个非常常用的类,该类用 ...
- java笔记--String类格式化当天日期转换符文档
String类格式化当天日期 --如果朋友您想转载本文章请注明转载地址"http://www.cnblogs.com/XHJT/p/3877389.html "谢谢-- 转换符:% ...
- 使用Python将字符串转换为格式化的日期时间字符串
我正在尝试将字符串“20091229050936”转换为“2009年12月29日(UTC)” >>>import time >>>s = time.strptime ...
- 深入理解Java:SimpleDateFormat安全的时间格式化
这一篇我什么都不写,只推荐一篇大牛的博客,这篇博客给了我很多灵感,让我对多线程理解的更加透彻了; http://www.cnblogs.com/chenying99/articles/3331950. ...
- http://www.cnblogs.com/peida/archive/2013/05/31/3070790.html深入理解Java:SimpleDateFormat安全的时间格式化
http://www.cnblogs.com/peida/archive/2013/05/31/3070790.html
- [java工具类01]__构建格式化输出日期和时间的工具类
在之前的学习中,我写过一篇关于字符串格式化的,就主要设计到了时间以及日期的各种格式化显示的设置,其主要时通过String类的fomat()方法实现的. 我们可以通过使用不同的转换符来实现格式化显示不同 ...
- string.Format 格式化输出日期
string.Format("{0:d}",System.DateTime.Now) 结果为:2009-3-20 (月份位置不是03) string.Format("{0 ...
- DateFormat类,利用SimpleDateFormat解决系统时间初始(格式化/解析)问题
目标: java.text.DateFormat 是日期/时间格式化子类的抽象类,我们通过这个类可以帮我们完成日期和文本之间的转换,也就是可以在Date对象与String对象之间进行来回转换. 格式化 ...
- Java之SimpleDateFormat日期格式转换(Date 和 String 类型之间的转换)
SimpleDateFormat : 可以选择任何用户定义的日期-时间格式的模式 "yyyy-MM-dd HH:mm:ss:SSS"1.格式化:Date -->Stri ...
随机推荐
- 【kmp算法】hdu4763 Theme Section
kmp中next数组的含义是:next[i]表示对于s[0]~s[i-1]这个前缀而言,最大相等的前后缀的长度是多少.规定next[0]=-1. 迭代for(int i=next[i];i!=-1;i ...
- 【数形结合】Gym - 100923I - Por Costel and the Pairs
perechi3.in / perechi3.out We don't know how Por Costel the pig arrived at FMI's dance party. All we ...
- 分析成绩 Exercise07_04
import java.util.Scanner; /** * @author 冰樱梦 * 时间:2018年下半年 * 题目:分析成绩 * */ public class Exercise07_04 ...
- 数列求和 Exercise06_13
/** * @author 冰樱梦 * 时间:2018年下半年 * 题目:数列求和 * */ public class Exercise06_13 { public static void main( ...
- Java二进制指令代码解析
http://www.blogjava.net/DLevin/archive/2011/09/13/358497.html http://blog.csdn.net/sum_rain/article/ ...
- Matlab设置形状大小
x=0:10; y=2*x; plot(x,y,'-*','linewidth',0.5,'markersize',6)%%默认线宽为0.5,点大小为6 说明:调整线宽也可改变点的形状,这实际上是通过 ...
- ActiveX控件在项目中的应用
- python 下载小说
以下载官场风月小说为例: 具体代码: # coding=utf-8 import os import re from selenium import webdriver from selenium.c ...
- 代码这样写更优雅(Python 版)(转载)
转载:https://mp.weixin.qq.com/s?timestamp=1498528588&src=3&ver=1&signature=DfFeOFPXy44ObCM ...
- LVS负载均衡之DR模式部署
1.LVS的DR模式介绍 参考自官网:http://www.linuxvirtualserver.org/zh/lvs3.html VS/DR利用大多数Internet服务的非对称特点,负 ...