1、java时间处理

package com.bmkit.util.date;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date; /**
* Created by root on 16-5-23.
*/
public class TestDate { /**
* format time
*/
private static void formatTime() {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date = new Date();
System.out.println(dateFormat.format(date));
} /**
* format time
*/
private static void formatTime1(Date date) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
System.out.println(dateFormat.format(date));
} /**
* parse string date to date
*/
private static void parseStringToDate() {
String sDate = "2016-05-23 03:07:32";
String sDate1 = "2016-05-23";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
Date date1 = null; try {
date = dateFormat.parse(sDate);
date1 = dateFormat1.parse(sDate1);
} catch (ParseException e) {
e.printStackTrace();
}
formatTime1(date);
formatTime1(date1); System.out.println(campareDate(date, date1));
} /**
* compare date return -1,0,1
*/
private static int campareDate(Date date1, Date date2) {
return date1.compareTo(date2);
} /**
* get time stamp System.currentTimeMillis() 这种方式速度最快,Calendar.getInstance().getTimeInMillis()
*/
private static void getTimeStamp() {
//方法 一
System.out.println(System.currentTimeMillis());
//方法 二
System.out.println(Calendar.getInstance().getTimeInMillis());
//方法 三
System.out.println(new Date().getTime());
} public static void getTimeByDate() {
Date date = new Date();
DateFormat df1 = DateFormat.getDateInstance();//日期格式,精确到日
System.out.println(df1.format(date));
DateFormat df2 = DateFormat.getDateTimeInstance();//可以精确到时分秒
System.out.println(df2.format(date));
DateFormat df3 = DateFormat.getTimeInstance();//只显示出时分秒
System.out.println(df3.format(date));
DateFormat df4 = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL); //显示日期,周,上下午,时间(精确到秒)
System.out.println(df4.format(date));
DateFormat df5 = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG); //显示日期,上下午,时间(精确到秒)
System.out.println(df5.format(date));
DateFormat df6 = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT); //显示日期,上下午,时间(精确到分)
System.out.println(df6.format(date));
DateFormat df7 = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM); //显示日期,时间(精确到分)
System.out.println(df7.format(date));
} public static void getTimeByCalendar() {
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);//获取年份
int month = cal.get(Calendar.MONTH);//获取月份
int day = cal.get(Calendar.DATE);//获取日
int hour = cal.get(Calendar.HOUR);//小时
int minute = cal.get(Calendar.MINUTE);//分
int second = cal.get(Calendar.SECOND);//秒
int WeekOfYear = cal.get(Calendar.DAY_OF_WEEK);//一周的第几天
System.out.println("现在的时间是:公元" + year + "年" + (month + 1) + "月" + day + "日 " + hour + "时" + minute + "分" + second + "秒 星期" + WeekOfYear);
} public static void main(String[] arg) {
//formatTime();
//parseStringToDate();
getTimeStamp();
//getTimeByDate();
//getTimeByCalendar();
}
}

2、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 时区
常见标准的写法"yyyy-MM-dd HH:mm:ss",注意大小写,时间是24小时制,24小时制转换成12小时制只需将HH改成hh,不需要另外的函数。

java之Date的更多相关文章

  1. java基础--java.util.Date类型小结

    首先先来了解一下Date数据类型: . Date类型通常要和另一个 java.text.SimpleDateFormat类联合使用. 把long-->Date: public Date(long ...

  2. 前台传参数时间类型不匹配:type 'java.lang.String' to required type 'java.util.Date' for property 'createDate'

    springMVC action接收参数: org.springframework.validation.BindException: org.springframework.validation.B ...

  3. Java中Date的比较(befor与after方法的缺陷)

    java.util.Date中的before和after方法只会比较到Day,不管你的date是yyyy-MM-dd HH:mm:ss还是yyyy-MM-dd格式的.最好用getTime()来比较具体 ...

  4. java.util.Date与java.sql.Date

    我数据库里用到了日期类型.用java编程的时候同时import了java.util.*和java.sql.*,发现直接申明Date类型 Date dt; 会报错,查了一下才发现有java.util.D ...

  5. Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property 'xxx': no matching editors or conversion strategy found

    今天在完成项目的时候遇到了下面的异常信息: 04-Aug-2014 15:49:27.894 SEVERE [http-apr-8080-exec-5] org.apache.catalina.cor ...

  6. java.util.Date和java.sql.Date的区别和相互转化

    java.util.Date是在除了SQL语句的情况下面使用的.java.sql.Date是针对SQL语句使用的,它只包含日期而没有时间部分它 们都有getTime方法返回毫秒数,自然就可以直接构建. ...

  7. java获取获得Timestamp类型的当前系统时间。以及java.util.date 、java.sql.Date之间的转换

    java获取取得Timestamp类型的当前系统时间java获取取得Timestamp类型的当前系统时间 格式:2010-11-04 16:19:42 方法1: Timestamp d = new T ...

  8. java中Date与String的相互转化

    1:大体思路 这种转换要用到java.text.SimpleDateFormat类 字符串转换成日期类型: 方法1: 也是最简单的方法 Date date=new Date("2008-04 ...

  9. java.util.Date和java.sql.Date的区别和相互转化(转)

    java.util.Date是在除了SQL语句的情况下面使用的.java.sql.Date是针对SQL语句使用的,它只包含日期而没有时间部分它们都有getTime方法返回毫秒数,自然就可以直接构建.  ...

随机推荐

  1. collectionView

    // /* UICollectionView 类是iOS6 新引进的API,用于展示集合视图, 布局更加灵活,可实现多列布局,用法类似于UITableView类. - 更新视图: [collectio ...

  2. HTTPS背后的加密算法

    当你在浏览器的地址栏上输入https开头的网址后,浏览器和服务器之间会在接下来的几百毫秒内进行大量的通信.InfoQ的这篇文章对此有非常详细的描述.这些复杂的步骤的第一步,就是浏览器与服务器之间协商一 ...

  3. mysql查看表使用的数据库引擎

    看某个使用的引擎,在显示结果里参数engine后面的就表示该表当前用的存储引擎: mysql> show create table 表名; 看mysql支持哪些存储引擎: mysql> s ...

  4. Oracle 的表备份的方法

    1.直接备份(防止误操作后数据库表不能恢复) create table new_table as select * from old_table; 2.创建表头,然后插入列(繁琐的做法) create ...

  5. re正则表达式13_review of regex symbols

    Review of Regex Symbols This chapter covered a lot of notation, so here’s a quick review of what you ...

  6. sqlserver插入时发生在“xxx”处关键发生错误

    今天知道了一个小技巧,当你的数据库表名为user时会sqlserver的表发生冲突,所以因该将user这样用[user],ok 一切搞定 .

  7. Centos6.5安装和使用docker

    rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm yum install docke ...

  8. php 生成随机字符串 abcdeft....789

    ) { $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; $str = &quo ...

  9. guid正则表达

    a-fA-F0-9 加上下划线 _ 可以用 \w 来代替. ^\w{8}-(\w{4}-){3}\w{12}$ 如果不可以用下划线, 0-9 用 \d 代替 a-fA-F 就用其中一个 a-f,然后匹 ...

  10. Database Initialization Strategies in Code-First:

    You already created a database after running your Code-First application the first time, but what ab ...