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的更多相关文章

  1. Kafka2.4发布——新特性介绍(附Java Api Demo代码)

    新功能 允许消费者从最近的副本进行获取 为 Consumer Rebalance Protocol 增加对增量协同重新均衡(incremental cooperative rebalancing)的支 ...

  2. ES系列十五、ES常用Java Client API

    一.简介 1.先看ES的架构图 二.ES支持的客户端连接方式 1.REST API http请求,例如,浏览器请求get方法:利用Postman等工具发起REST请求:java 发起httpClien ...

  3. 关于c#调用java中间件api的几个问题

    由于项目需要,做的c#客户端数据库连接串首先肯定不能写死的程序里(数据库很容易被攻击,我们的项目半年改了几次密码...) 放置在配置文件内,都可以看得到,最开始想法将配置文件加密,老师说加密过的文件还 ...

  4. JAVA长连接demo

    http://blog.csdn.net/caomiao2006/article/details/38830475 JAVA长连接demo 2014-08-25 23:20 4767人阅读 评论(2) ...

  5. Java Date Classes

    References: [1] http://tutorials.jenkov.com/java-date-time/index.html [2] https://docs.oracle.com/ja ...

  6. Java Servlet API中文说明文档

    Java Servlet API中文说明文档 目 录 1.... Servet资料 1.1      绪言 1.2      谁需要读这份文档 1.3      Java Servlet API的组成 ...

  7. Java Collections API和泛型

    Java Collections API和泛型 数据结构和算法 学会一门编程语言,你可以写出一些可以工作的代码用计算机来解决一些问题,然而想要优雅而高效的解决问题,就要学习数据结构和算法了.当然对数据 ...

  8. 乐字节-Java8新特性之Date API

    上一篇文章,小乐给大家带来了Java8新特性之Optional,接下来本文将会给大家介绍Java8新特性之Date API 前言: Java 8通过发布新的Date-Time API来进一步加强对日期 ...

  9. 011-jdk1.8版本新特性三-Date API

    1.7.Date API Java 8 在包java.time下包含了一组全新的时间日期API.新的日期API和开源的Joda-Time库差不多,但又不完全一样,下面的例子展示了这组新API里最重要的 ...

随机推荐

  1. android中的返回键与Activity

          我在做应用时遇到一个问题.就是在启动主页面时须要预先载入一些数据.我是在一个载入页中处理完这些数据然后再转到主页面.但当我在主页面中按返回键时,系统会返回载入页面.我不希望载入页在使用完之 ...

  2. Codeforces 32E Hide-and-Seek 乞讨2关于镜面反射点 计算几何

    主题链接:点击打开链接 必须指出的是,反射镜和2个人共线是不是障碍,但根据该壁其他情况 #include<cstdio> #include<iostream> #include ...

  3. Red Gate系列之七 SQL Search 1.1.6.1 Edition SQL查询分析工具使用教程

    原文:Red Gate系列之七 SQL Search 1.1.6.1 Edition SQL查询分析工具使用教程 Red Gate系列之七 SQL Search 1.1.6.1 Edition SQL ...

  4. view和activity的区别(转)

    activity相当于控制部分,view相当于显示部分.两者之间是多对多的关系,所有东西必须用view来显示. viewGroup继承自view,实现了ViewManager,ViewParent接口 ...

  5. [原创] linux deepin 2014.1下编译putty

    在网上找了很久,都没有找到linux下直接可以用的putty程序,最终在putty官网找到了源代码 点击下载 把源代码下载回来. 1.下载源代码 2.安装依赖库 如果系统中没有安装过libgtk2.0 ...

  6. ORACLE Install (10g r2) FOR Red Hat Enterprise Linux Server release 5.5 (64 bit) (转)

    OS Info----------# cat /etc/redhat-releaseRed Hat Enterprise Linux Server release 5.5 (Tikanga)# cat ...

  7. swift的struct本节描述结构的类型

    <span style="font-size:24px;">struct David { var x = 0;//一个结构的定义,两个字段x,y var y = 0;/ ...

  8. 3g自己主动更新网卡驱动web完架构文档

    几年前写. 看它是否是用得上 1  简单介绍 本文档具体描写叙述了基于ASP.NET平台和IIS服务的T-Mobile自己主动更新系统的实现框架. 本文档主要从技术架构和业务架构两个方面来着手来描写叙 ...

  9. 区域及分离、Js压缩、css、jquery扩展

    后台管理区域及分离.Js压缩.css.jquery扩展 本系列目录:ASP.NET MVC4入门到精通系列目录汇总 有好一段时间没更新博文了,最近在忙两件事:1.看书,学习中...2.为公司年会节目做 ...

  10. python udp编程实例

    与python tcp编程控制见 http://blog.csdn.net/aspnet_lyc/article/details/39854569 c++ udp/tcp 编程见 http://blo ...