SimpleDateFormat关于时间类的一些常用处理
项目中经常会出现对时间类的一些处理,记录一下:
实例一:
/**
* 获取当前时间是星期几?
*
* @param args
*/
public static void main(String[] args) {
SimpleDateFormat format = new SimpleDateFormat("E");
Date date = new Date();
String s = format.format(date);
System.out.println("s:"+s);
}
s:星期四
实例二:
@Test
public void Test(){
Date date = new Date();
SimpleDateFormat weekFormat = new SimpleDateFormat("E");
System.out.println(weekFormat.format(date)); SimpleDateFormat monthFormat = new SimpleDateFormat("MMMM");
System.out.println(monthFormat.format(date)); SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy");
System.out.println(yearFormat.format(date)); SimpleDateFormat dayFormat = new SimpleDateFormat("dd");
System.out.println(dayFormat.format(date)); /**
*
星期三
五月
2017
24
*
*/
}
实例三:
@Test
public void Test1(){
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());
}
2017年05月25日 21时23分11秒
17/05/25 21:23
2017-05-25 21:23:11
2017年05月25日 21时23分11秒 星期四
一年中的第 145 天 一年中第21个星期 一月中第4个星期 在一天中21时 CST时区
25 May 2017 13:23:11 GMT
2017-5-25 21:23:11
Thu May 25 21:23:11 CST 2017
以上!!!
SimpleDateFormat关于时间类的一些常用处理的更多相关文章
- JDK中日期和时间的几个常用类浅析(三)
java.text.SimpleDateFormat SimpleDateFormat类是用于把字符串解析成日期时间和把日期时间格式化成字符串的工具类.该类主要和java.util.Date类配合 ...
- js(jQuery)获取时间的方法及常用时间类搜集
获取时间的方法及常用时间类都是大家经常使用的,在本文为大家整理了一些,个人感觉还比较全,感兴趣的朋友可以收集下 复制代码代码如下: $(function(){ var mydate = new D ...
- [19/03/16-星期六] 常用类_Date时间类&DateFormat类
一.Date时间类 计算机中 以1970 年 1 月 1 日 00:00:00定为基准时间,每个度量单位是毫秒(1秒的千分之一) 用ong类型的变量来表示时间,如当前时刻数值:long now =n ...
- 常用类--Date日期类,SimpleDateFormat日期格式类,Calendar日历类,Math数学工具类,Random随机数类
Date日期类 Date表示特定的时间,精确到毫秒; 构造方法: public Data() public Date(long date) 常用方法: public long getTime() pu ...
- Java 学习 时间格式化(SimpleDateFormat)与历法类(Calendar)用法详解
基于Android一些时间创建的基本概念 获取当前时间 方式一: Date date = new Date(); Log.e(TAG, "当前时间="+date); 结果: E/T ...
- (总结)工作中常用的js自定义函数——日期时间类
//设置时间类 var Wsdatatime = function(){ this.today = (new Date()).getTime(); //当前时间 } Wsdatatime.protot ...
- JDK中日期和时间的几个常用类浅析(四)
java.time.Instant java.time.Instant类对应的是时间线上的一个时间点.该类通过保存着从格林威治的起始时间(1970年一月一日零点零分)开始计算所经过的纳妙数来表示时 ...
- JDK中日期和时间的几个常用类浅析(五)
LocalDateTime LocalDateTime是JDK8中才引入的类,用来表示不包含时区信息的本地日期和时间.我们可以把LocalDateTime看作是LocalDate和LocalTim ...
- Java 线程安全LocalTime 和LocaldateTime 新的Date和Time类 -JDK8新时间类的简单使用
不可变类且线程安全 LocalDate .java.time.LocalTime 和LocaldateTime 新的Date和Time类 DateTimeFormatter ==https://ww ...
随机推荐
- C# 访问修饰符internal的访问范围误区释疑
一.前言 MSDN关于访问修饰符的访问级别解释: 访问修饰符是一些关键字,用于指定声明的成员或类型的可访 ...
- [学习笔记]Senparc.CO2NET 缓存使用笔记
>笔记1:如果需要调用远程的Redis,那么您需要2步 步骤1: 在项目的web.config文件中配置 <!-- Cache.Redis连接配置 --> <add key=& ...
- Git基本用法简介
一. git和svn的主要区别 git是一个分布式的版本控制工具,而svn是一个集中式版本控制工具. 二. git工具安装 首先下载git:https://gi ...
- Lesson9 some interesting things in C#
1.关键帧动画 1)xml 界面 <Page x:Class="Test.MainPage" xmlns="http://schemas.microsoft.com ...
- C语言.c和.h
简单的说其实要理解C文件与头文件(即.h)有什么不同之处,首先需要弄明白编译器的工作过程,一般说来编译器会做以下几个过程: 1.预处理阶段 2.词法与语法分析阶段 3.编译阶段,首先编译成 ...
- 重建二叉树_C++
一.题目背景 给定一个二叉树的前序和中序遍历,求出它的后序遍历 二叉树的遍历可参考 http://blog.csdn.net/fansongy/article/details/6798278/ 二.算 ...
- jQuery操作下拉列表以及单选多选框
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- centos 7 安装配置mod_security
1.旧版本安装过程: http://blog.secaserver.com/2011/10/install-mod_security-apache2-easiest/ http://www.cnblo ...
- Windows ToolTips简要介绍(转)
原文转自 https://blog.csdn.net/sesiria/article/details/77450151 Windows 标准控件ToolTips简要介绍 参考文档 MSDN https ...
- MFC/C++/C中字符类型CString, int, string, char*之间的转换
1 CString,int,string,char*之间的转换 string 转 CString CString.format("%s", string.c_str()); cha ...