java时间格式的转换
2016-11-05 23:26
330 查看
package com.example.k.myapplication; import android.annotation.TargetApi; import android.os.Build; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.format.DateFormat; import android.util.Log; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); try { Date date = simpleDateFormat.parse("2016-11-5 10:10"); Date date_2 = simpleDateFormat.parse("2016-11-6 10:10"); long a = date.getTime(); long b = date_2.getTime(); Log.i("test","相差"+(b-a)+"毫秒"); } catch (ParseException e) { e.printStackTrace(); } } }
</pre><pre code_snippet_id="1968781" snippet_file_name="blog_20161105_1_7597323" name="code" class="html">
<dl style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><dt>public class <strong>SimpleDateFormat</strong> extends DateFormat</dt></dl><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><code>SimpleDateFormat</code> 是一个以国别敏感的方式格式化和分析数据的具体类。 它允许格式化 (date -> text)、语法分析 (text -> date)和标准化。</p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><code>SimpleDateFormat</code> 允许以为日期-时间格式化选择任何用户指定的方式启动。 但是,希望用 <code>DateFormat</code> 中的<code>getTimeInstance</code>、 <code>getDateInstance</code> 或 <code>getDateTimeInstance</code> 创建一个日期-时间格式化程序。 每个类方法返回一个以缺省格式化方式初始化的日期/时间格式化程序。 可以根据需要用 <code>applyPattern</code> 方法修改格式化方式。 SimpleDateFormat函数的继承关系: <span style="color:#000080;"><a target=_blank href="http://lib.csdn.net/base/17" class="replace_word" title="Java EE知识库" target="_blank" style="color: rgb(223, 52, 52); text-decoration: none; font-weight: bold;">Java</a>.lang.Object | +----java.text.Format | +----java.text.DateFormat | +----java.text.SimpleDateFormat</span> 下面是个小例子: import java.text.*; import java.util.Date;</p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">/** SimpleDateFormat函数语法: G 年代标志符 y 年 M 月 d 日 h 时 在上午或下午 (1~12) H 时 在一天中 (0~23) m 分 s 秒 S 毫秒 E 星期 D 一年中的第几天 F 一月中第几个星期几 w 一年中第几个星期 W 一月中第几个星期 a 上午 / 下午 标记符 k 时 在一天中 (1~24) K 时 在上午或下午 (0~11) z 时区 */ public class FormatDateTime {</p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"> public static void main(String[] args) { SimpleDateFormat myFmt=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒"); SimpleDateFormat myFmt1=new SimpleDateFormat("yy/MM/dd HH:mm"); SimpleDateFormat myFmt2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//等价于now.toLocaleString() SimpleDateFormat myFmt3=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒 E "); SimpleDateFormat myFmt4=new SimpleDateFormat( "一年中的第 D 天 一年中第w个星期 一月中第W个星期 在一天中k时 z时区"); Date now=new Date(); System.out.println(myFmt.format(now)); System.out.println(myFmt1.format(now)); System.out.println(myFmt2.format(now)); System.out.println(myFmt3.format(now)); System.out.println(myFmt4.format(now)); System.out.println(now.toGMTString()); System.out.println(now.toLocaleString()); System.out.println(now.toString()); } } 效果: 2004年12月16日 17时24分27秒 04/12/16 17:24 2004-12-16 17:24:27 2004年12月16日 17时24分27秒 星期四 一年中的第 351 天 一年中第51个星期 一月中第3个星期 在一天中17时 CST时区 16 Dec 2004 09:24:27 GMT 2004-12-16 17:24:27 Thu Dec 16 17:24:27 CST 2004 下面是个JavaBean: public class FormatDateTime { public static String toLongDateString(Date dt){ SimpleDateFormat myFmt=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒 E "); return myFmt.format(dt); } public static String toShortDateString(Date dt){ SimpleDateFormat myFmt=new SimpleDateFormat("yy年MM月dd日 HH时mm分"); return myFmt.format(dt); } public static String toLongTimeString(Date dt){ SimpleDateFormat myFmt=new SimpleDateFormat("HH mm ss SSSS"); return myFmt.format(dt); } public static String toShortTimeString(Date dt){ SimpleDateFormat myFmt=new SimpleDateFormat("yy/MM/dd HH:mm"); return myFmt.format(dt); } public static void main(String[] args) {</p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"> Date now=new Date();</p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"> System.out.println(FormatDateTime.toLongDateString(now)); System.out.println(FormatDateTime.toShortDateString(now)); System.out.println(FormatDateTime.toLongTimeString(now)); System.out.println(FormatDateTime.toShortTimeString(now)); } } 调用的main 测试结果: 2004年12月16日 17时38分26秒 星期四 04年12月16日 17时38分 17 38 26 0965 04/12/16 17:38</p>
相关文章推荐
- Java中常用的一种时间格式的转换方法
- Java将Unix时间戳转换成指定格式日期
- java.util.Date 与 java.sql.Date互转 及 字符串转换为日期时间格式
- php时间戳转换成java格式
- 我的Java开发学习之旅------>工具类:将播放器的进度值转换成相应的时间格式
- java.util.Date 与 java.sql.Date互转 及 字符串转换为日期时间格式
- java 时间格式的转换
- java.util.Date_与_java.sql.Date互转_及_字符串转换为日期时间格式
- java 时间格式 转换大全 终结版
- java 时间格式转换及日期比较
- java中字符串转换成时间格式总结
- Java时间格式的转换
- 在java中如何把字符串转换成时间格式?
- Java DATE类型日期转换为指定格式――取出时间与系统时间差8小时
- java中字符串转换成时间格式总结
- 在Java中获取时间,输出格式设置和与String的转换
- java.util.Date_与_java.sql.Date互转_及_字符串转换为日期时间格式
- Java时间处理 将时间戳转换为“几天前”的格式
- Java时间格式转换
- Java中时间戳转换成时间格式的方法