java 设定时间 操作_java时间日期操作

时间日期类:

package org;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

public class DateUtil {

Calendar cal = Calendar.getInstance();

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

public Date getFormatDate(String date){

try {

return sdf.parse(date);

} catch (ParseException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return null;

}

/**

* 格式化时间日期

* @return

*/

public String getFormatDate(){

Date d = new Date();

return sdf.format(d);

}

/**

* 获取当前的时间: xxxx年xx月xx日xx:xx:xx

* @return

*/

public String getCurrentDate(){

return cal.get(Calendar.YEAR) + "年" + (cal.get(Calendar.MONTH)+1) + "月" + cal.get(Calendar.DAY_OF_MONTH) + "日 " + cal.get(Calendar.HOUR_OF_DAY) + ":" + cal.get(Calendar.MINUTE )+ ":" + cal.get(Calendar.SECOND);

}

/**

* 获取当前分钟中的秒

* @return

*/

public int getMillion(){

return cal.get(Calendar.SECOND);

}

/**

* 获取当前小时中的分钟

* @return

*/

public int getMinute(){

return cal.get(Calendar.MINUTE);

}

/**

* 获取当天的第几个小时

* @return

*/

public int getHour(){

return cal.get(Calendar.HOUR_OF_DAY);

}

/**

* 获取当前月中的第几天

* @return

*/

public int getDay(){

return cal.get(Calendar.DAY_OF_MONTH);

}

/**

* 获取当前的月份

* @return

*/

public int getMonth(){

return (cal.get(Calendar.MONTH)+1);

}

/**

* 获取当前的年份

* @return

*/

public int getYear(){

return cal.get(Calendar.YEAR);

}

/**

* 获取某月的第几个周

* @return int

*/

public int getWeekOfMonth(){

return cal.WEEK_OF_MONTH;

}

/**

* 获取当前的时间毫秒数

* @return

*/

public long getCurrenTimeMillis(){

return System.currentTimeMillis();

}

}Main 方法:

public static void main(String[] args) {

DateUtil d = new DateUtil();

System.out.println("字符串转换当前的时间是:"+d.getFormatDate("2016-11-27 12:45:07"));

System.out.println("当前的时间是:"+d.getFormatDate());

System.out.println("获取当前的时间:"+d.getCurrentDate());

System.out.println(new Date(d.getCurrenTimeMillis()));

System.out.println("获取某月的第几个星期:"+d.getWeekOfMonth());

System.out.println("获取当前时间的毫秒数:"+d.getCurrenTimeMillis()+" ms");

}打印的结果集:

字符串转换当前的时间是:Sun Nov 27 12:45:07 GMT+08:00 2016

当前的时间是:2016-11-27 12:50:33

获取当前的时间:2016年11月27日  12:50:33

Sun Nov 27 12:50:33 GMT+08:00 2016

获取某月的第几个星期:4

获取当前时间的毫秒数:1480222233392 ms

简单的DateFormat格式代码:

要指定时间格式,使用时间模式字符串,在这个模式中,所有的ASCII字母被保留为模式字母

定义如下:

Character

描述

Example

G

Era designator

AD

y

Year in four digits

2001

M

Month in year

July or 07

d

Day in month

10

h

Hour in A.M./P.M. (1~12)

12

H

Hour in day (0~23)

22

m

Minute in hour

30

s

Second in minute

55

S

Millisecond

234

E

Day in week

Tuesday

D

Day in year

360

F

Day of week in month

2 (second Wed. in July)

w

Week in year

40

W

Week in month

1

a

A.M./P.M. marker

PM

k

Hour in day (1~24)

24

K

Hour in A.M./P.M. (0~11)

10

z

Time zone

Eastern Standard Time

'

Escape for text

Delimiter

"

Single quote

`

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值