java Date获取 年月日时分秒


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

package com.util;

import java.text.DateFormat;

import java.util.Calendar;

import java.util.Date;

public class Test {

    public 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 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+"月"+day+"日      "+hour+"时"+minute+"分"+second+"秒       星期"+WeekOfYear);

    }

    public static void main(String[] args) {

        Test t=new Test();

        t.getTimeByDate();

        System.out.println("****************************");

        t.getTimeByCalendar();

    }

}

  

获取日,如果大于16则+2个月,否则+1个月,输出7个月


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

public static void main(String[] as) throws Exception {

    int j;

    Calendar cc = Calendar.getInstance();

    int myYear = cc.get(Calendar.YEAR);

    int myMonth = cc.get(Calendar.MONTH);

    j=cc.get(Calendar.DATE)>16?3:2;

    cc.set(Calendar.MONTH, myMonth+j);

    System.out.println(myYear+"-"+cc.get(cc.MONTH));

    for(int i=0;i<7;i++){

        cc.add(cc.MONTH, 1);

        int mm = cc.get(cc.MONTH);

        int mmm=mm==0?12:mm;

        System.out.println(myYear+"-"+mmm);

    }

}

  

这里有个博客http://www.blogjava.net/xiaoyi/articles/295044.html,里面有一些常用方法的实现,可以参考,自己在优化

时间: 2024-08-25 07:21:12

java Date获取 年月日时分秒的相关文章

获取年月日时分秒

NSDate *now = [NSDate date]; NSCalendar *cal = [NSCalendar currentCalendar]; unsigned int unitFlags = NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit; NSDateComponents *dd = [cal components:unitFlags fromDate:

sqlserver 获取年月日 时分秒

select GETDATE() as '当前日期',DateName(year,GetDate()) as '年',DateName(month,GetDate()) as '月',DateName(day,GetDate()) as '日',DateName(dw,GetDate()) as '星期',DateName(week,GetDate()) as '周数',DateName(hour,GetDate()) as '时',DateName(minute,GetDate()) as '

Android Calendar获取年月日时分秒毫秒

开始使用new Date()测试,并用通过date.getMonth(),和date.getDay()获取,不过后来发现这两个访求是jdk1.1版本的,现在已经不用了,而且结果也不正确. int month = (date.get(Calendar.MONTH))+1; int day = date.get(Calendar.DAY_OF_MONTH); 获取当前的月份和日期 试了一下,果然正确 后来查看java doc文档,MONTH字段解释如下 Field number for get an

java Date日期去掉时分秒

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");String s = sdf.format(new Date());Date date = sdf.parse(s); 来自为知笔记(Wiz)

获取年月日 时分秒

Calendar calendar = Calendar.getInstance(); int year = calendar.get(Calendar.YEAR); // 年 System.out.println(year); int month = calendar.get(Calendar.MONTH) + 1; // 月 System.out.println(month); int day = calendar.get(Calendar.DATE); // 日 System.out.pr

js获取年月日时分秒星期

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Typ

原生JS代码封装(获取年月日时分秒 )

function numberDoubled(n){ n = n+""; return n.length==1?"0"+n:n; } function date2string(d, sp){ sp = sp || "-"; var year = d.getFullYear(); var month = d.getMonth()+1; var date = d.getDate(); var hour = d.getHours(); var min

Java计算年月日时分秒时间差(两个时间相减)

//测试主方法  public static void main(String[] args) {          Date currentTime = df.parse("2004-03-26 13:31:40");   //当前系统时间             Date firstTime = df.parse("2004-01-02 11:30:24");     //查询的数据时间          String str=getTime(currentTi

sql server获取当前年月日 时分秒

获取当前年月日(字符串): select CONVERT(varchar(11),GETDATE(),112) 获取当前时间的时分秒(':'隔开): select CONVERT(varchar(12),GETDATE(),108) 将年月日时分秒拼接成一条字符串: select CONVERT(varchar(11),GETDATE(),112)+REPLACE(CONVERT(varchar(12),GETDATE(),108),':','')