java SimpleDateFormat日期与时间戳的相互转换
自我总结,有什么不到位的地方,各位可以帮忙纠正补充一下,感激不尽!
目的:SimpleDateFormat类可以很随意的组合日期时间的格式,不止单纯的yyyy-MM-dd这种格式
废话不多说,上代码
测试类 DateTest
package com.core.test; import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date; public class DateTest { public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf0 = new SimpleDateFormat("yyyyMMdd");
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日");
SimpleDateFormat sdf3 = new SimpleDateFormat("ddMMyyyy"); String time0 = "20140916";
String time1 = "2014-09-16";
String time2 = "2014年09月16日";
String time3 = "16092014"; convertStamp(sdf0, time0);
convertStamp(sdf1, time1);
convertStamp(sdf2, time2);
convertStamp(sdf3, time3); long stamp = 1410796800000L;
convertDate(sdf0, stamp);
convertDate(sdf1, stamp);
convertDate(sdf2, stamp);
convertDate(sdf3, stamp);
} // 日期转换成时间戳
public static void convertStamp(SimpleDateFormat format, String time)throws ParseException {
Date date = format.parse(time);
System.out.println(date.getTime());
} // 时间戳转换成日期
public static void convertDate(SimpleDateFormat sdf, long stamp) {
String date = sdf.format(new Date(stamp));
System.out.println(date);
} }
运行结果:
1410796800000
1410796800000
1410796800000
1410796800000
20140916
2014-09-16
2014年09月16日
16092014
SimpleDateFormat很灵活吧,哈哈
java SimpleDateFormat日期与时间戳的相互转换的更多相关文章
- java中时间与时间戳的相互转换
package com.test.one; import java.text.ParseException; import java.text.SimpleDateFormat; import jav ...
- Java SimpleDateFormat 日期-时间格式参数
字母 日期或时间元素 表示 示例 G Era 标志符 Text AD y 年 Year 1996; 96 M 年中的月份 Month ...
- python中时间、日期、时间戳的转换
1.简介 在编写代码时,往往涉及时间.日期.时间戳的相互转换. 2.示例 # 引入模块 import time, datetime 2.1 str类型的日期转换为时间戳 # 字符类型的时间 tss1 ...
- java 日期转时间戳,时间戳转为日期
package date; import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Dat ...
- java Data、String、Long三种日期类型之间的相互转换
java Data.String.Long三种日期类型之间的相互转换 // date类型转换为String类型 // formatType格式为yyyy-MM-dd HH:mm:ss// ...
- Java SimpleDateFormat处理日期与字符串的转换
1.为什么要使用SimpleDateFormat? 在Java中,如果我们想获取当前时间,一般会使用Date类的无参构造函数,如下所示,我们获取到当前时间并输出: import java.util.D ...
- java 日期获取时间戳
SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss"); String dateS ...
- java比较日期大小及日期与字符串的转换【SimpleDateFormat操作实例】
java比较日期大小及日期与字符串的转换[SimpleDateFormat操作实例] package com.ywx.test; import java.text.ParseException; im ...
- 【转】JAVA 8 日期/时间(Date Time)API指南
前言 本来想写下Java 8的日期/时间API,发现已经有篇不错的文章了,那就直接转载吧~ PS:主要内容没变,做了部分修改. 原文链接: journaldev 翻译: ImportNew.com - ...
随机推荐
- 爬取网页内容java
下面介绍的这个方法只是作为抛砖引玉:根据网页URL就可以抓取其中的内容 /** * @title getHtmlResourceByUrl * @param url 网址 * @param encod ...
- Python_day1
一.HelloWorld >>>print("Hello World!") >>>Hello World! 二.变量 1.什么是变量 : ...
- python批量处理
# -*- coding: utf-8 -*- """ Created on Sat Jun 20 19:36:34 2015 @author: chaofn " ...
- deeplearning.ai 神经网络和深度学习 week2 神经网络基础 听课笔记
1. Logistic回归是用于二分分类的算法. 对于m个样本的训练集,我们可能会习惯于使用for循环一个个处理,但在机器学习中,是把每一个样本写成一个列向量x,然后把m个列向量拼成一个矩阵X.这个矩 ...
- Spring(一)Spring的第一滴血
前言 开始工作了,但是一进来公司本来是做爬虫和数据分析的,但是走了一个后端的,导致我必须要去顶替他的工作.因为这个项目使用的是Spring. SpringMVC.Hibernate所以我又要去回忆一下 ...
- ex_gcd(个人模版)
ex_gcd: #include<stdio.h> #include<string.h> using namespace std; int x,y; int ex_gcd(in ...
- BZOJ 1088: [SCOI2005]扫雷Mine【思维题,神奇的模拟+枚举】
1088: [SCOI2005]扫雷Mine Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3791 Solved: 2234[Submit][St ...
- 4395: [Usaco2015 dec]Switching on the Lights
每次到达一个点,或者点亮一个房间的灯的时候,检查一下它四周的点能否走. 一开始看错题了..要求的是最多能开多少房的灯. #include<cstdio> #include<iostr ...
- hdu_1025(LIS Nlog(N)算法)
题意:自己慢慢读吧.大概就是道路两边建路,给出建路需求,要求两条路不能有交叉,问最多可以建多少条路. 题解:一看数据范围500000,应该是dp,再画个图模拟一下,发现实质就是求最长上升子序列,很自然 ...
- 解决Perhaps you are running on a JRE rather than a JDK?问题
Maven-No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JD ...