Java Date API demo
package date; import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date; /*2015-9-9*/
public class DateDemo { /**
* @param args
*/
public static void main(String[] args) {
Date now = new Date();
System.out.println("用Date方式显示时间: " + now);//此方法显示的结果和Calendar.getInstance().getTime()一样 DateFormat d1 = DateFormat.getDateInstance(); //默认语言(汉语)下的默认风格(MEDIUM风格,比如:2008-6-16 20:54:53)
String str1 = d1.format(now);
System.out.println("用DateFormat.getDateInstance()格式化时间后为:" + str1); DateFormat d2 = DateFormat.getDateTimeInstance();
String str2 = d2.format(now);
System.out.println("用DateFormat.getDateTimeInstance()格式化时间后为:" + str2); DateFormat d3 = DateFormat.getTimeInstance();
String str3 = d3.format(now);
System.out.println("用DateFormat.getTimeInstance()格式化时间后为:" + str3); DateFormat d4 = DateFormat.getInstance(); //使用SHORT风格显示日期和时间
String str4 = d4.format(now);
System.out.println("用DateFormat.getInstance()格式化时间后为:" + str4); DateFormat d5 = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL); //显示日期,周,时间(精确到秒)
String str5 = d5.format(now);
System.out.println("用DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL)格式化时间后为:" + str5); DateFormat d6 = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG); //显示日期。时间(精确到秒)
String str6 = d6.format(now);
System.out.println("用DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG)格式化时间后为:" + str6); DateFormat d7 = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT); //显示日期,时间(精确到分)
String str7 = d7.format(now);
System.out.println("用DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT)格式化时间后为:" + str7); DateFormat d8 = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM); //显示日期,时间(精确到分)
String str8 = d8.format(now);//与SHORT风格相比,这种方式最好用
System.out.println("用DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM)格式化时间后为:" + str8); DateFormat d9=DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.MEDIUM);
String str9=d9.format(now);
System.out.println("DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.MEDIUM)"+str9); Calendar ca = Calendar.getInstance();
int year = ca.get(Calendar.YEAR);//获取年份
int month = ca.get(Calendar.MONTH);//获取月份
int day = ca.get(Calendar.DATE);//获取日
int minute = ca.get(Calendar.MINUTE);//分
int hour = ca.get(Calendar.HOUR);//小时
int second = ca.get(Calendar.SECOND);//秒
int WeekOfYear = ca.get(Calendar.DAY_OF_WEEK);
System.out.println("用Calendar.getInstance().getTime()方式显示时间: " + ca.getTime());
System.out.println("用Calendar获得日期是:" + year + "年" + month + "月" + day + "日");
System.out.println("用Calendar获得时间是:" + hour + "时" + minute + "分" + second + "秒");
System.out.println(WeekOfYear);//显示今天是一周的第几天(我做的这个例子正好是周二,故结果显示2,如果你再周6运行,那么显示6) } }
输出:
用Date方式显示时间: Wed Sep 09 21:37:04 CST 2015
用DateFormat.getDateInstance()格式化时间后为:2015-9-9
用DateFormat.getDateTimeInstance()格式化时间后为:2015-9-9 21:37:04
用DateFormat.getTimeInstance()格式化时间后为:21:37:04
用DateFormat.getInstance()格式化时间后为:15-9-9 下午9:37
用DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL)格式化时间后为:2015年9月9日 星期三 下午09时37分04秒 CST
用DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG)格式化时间后为:2015年9月9日 下午09时37分04秒
用DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT)格式化时间后为:15-9-9 下午9:37
用DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM)格式化时间后为:2015-9-9 21:37:04
DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.MEDIUM)2015年9月9日 21:37:04
用Calendar.getInstance().getTime()方式显示时间: Wed Sep 09 21:37:04 CST 2015
用Calendar获得日期是:2015年8月9日
用Calendar获得时间是:9时37分4秒
4
Java Date API demo的更多相关文章
- Kafka2.4发布——新特性介绍(附Java Api Demo代码)
新功能 允许消费者从最近的副本进行获取 为 Consumer Rebalance Protocol 增加对增量协同重新均衡(incremental cooperative rebalancing)的支 ...
- ES系列十五、ES常用Java Client API
一.简介 1.先看ES的架构图 二.ES支持的客户端连接方式 1.REST API http请求,例如,浏览器请求get方法:利用Postman等工具发起REST请求:java 发起httpClien ...
- 关于c#调用java中间件api的几个问题
由于项目需要,做的c#客户端数据库连接串首先肯定不能写死的程序里(数据库很容易被攻击,我们的项目半年改了几次密码...) 放置在配置文件内,都可以看得到,最开始想法将配置文件加密,老师说加密过的文件还 ...
- JAVA长连接demo
http://blog.csdn.net/caomiao2006/article/details/38830475 JAVA长连接demo 2014-08-25 23:20 4767人阅读 评论(2) ...
- Java Date Classes
References: [1] http://tutorials.jenkov.com/java-date-time/index.html [2] https://docs.oracle.com/ja ...
- Java Servlet API中文说明文档
Java Servlet API中文说明文档 目 录 1.... Servet资料 1.1 绪言 1.2 谁需要读这份文档 1.3 Java Servlet API的组成 ...
- Java Collections API和泛型
Java Collections API和泛型 数据结构和算法 学会一门编程语言,你可以写出一些可以工作的代码用计算机来解决一些问题,然而想要优雅而高效的解决问题,就要学习数据结构和算法了.当然对数据 ...
- 乐字节-Java8新特性之Date API
上一篇文章,小乐给大家带来了Java8新特性之Optional,接下来本文将会给大家介绍Java8新特性之Date API 前言: Java 8通过发布新的Date-Time API来进一步加强对日期 ...
- 011-jdk1.8版本新特性三-Date API
1.7.Date API Java 8 在包java.time下包含了一组全新的时间日期API.新的日期API和开源的Joda-Time库差不多,但又不完全一样,下面的例子展示了这组新API里最重要的 ...
随机推荐
- 基于KMP与Levenshtein模糊匹配算法的银行联行号查询(转)
在人民银行那里,每个银行的每一个营业网点都有自己唯一的银行联行号,根据这个号码能快速定位一间银行具体的分支行,就像根据一个身份证号码能快速确定一个人一样.例如汇款时,汇款单上要求填写收款人开户行,然后 ...
- HTTP相关概念
最近观看HTTP权威指南.这本书是一个小更,欲了解更多详细信息,我们不能照顾.但一些基本概念仍然应该清楚.在这里,我整理: HTTP--因特网的多媒体信使 HTTP 使用的是可靠的传输数据协议,因此即 ...
- Xamarin之 环境错误集锦
错误信息: connection of the layout renderer failed.this may be caused by a misconfiguration of java .p ...
- opencv环境的搭建,并打开一个本地PC摄像头。
1.opencv环境结构 推荐连结 http://www.cnblogs.com/Anykong/archive/2011/04/06/Anykong_OpenCV1.html 2.以下是基本測试,和 ...
- style.display table-row与block
<tr id="js_rowShow" style=" display:none"> </tr> 问题: display:设置成bloc ...
- Android 浏览器开发WebView setBlockNetworkImage本末
含义本身防止网络数据图片 webSettings.setBlockNetworkImage(true); 停止发布数据 webSettings.setBlockNetworkImage(false); ...
- 江湖急救篇:slave 复制错误
这样的事情是,我们DBA的一个暂时表,导致复制出错 老大给力,江湖救急. 关于该參数.淘宝丁奇写了篇文章还不错:MySQL小误区:关于set global sql_slave_skip_counter ...
- 进程和线程之间的关系和区别 和 CPU牒
流程具有一定独立功能的程序关于某个数据集合上的一次执行活动,进程是系统进行资源分配和调度的一个独立单位. 线程是进程的一个实体,是CPU调度和分派的基本单位,它是比进程更小的能独立执行的基本单位. 进 ...
- Chapter 1 Securing Your Server and Network(2):管理服务的SIDs
原文出处:http://blog.csdn.net/dba_huangzj/article/details/37927319 ,专题文件夹:http://blog.csdn.net/dba_huang ...
- node.js基础:模块的创建和引入
模块可能是一个文件,也可能是包含一个或多个文件的目录.如果模块是个目录,node.js通常会在这个目录下找一个叫index.js的文件作为模块的入口. 典型的模块是一个包含exports对象属性定义的 ...