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,里面有一些常用方法的实现,可以参考,自己在优化

  

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

  1. jquery获取年月日时分秒当前时间

    获取年月日时分秒的当前时间,可按照某种格式显示出来,下面是一种得到如2017年02月02日  00:00:00格式的当前时间 function getCurrentDate(date){ var y ...

  2. Sql 中获取年月日时分秒的函数

    getdate():获取系统当前时间 dateadd(datepart,number,date):计算在一个时间的基础上增加一个时间后的新时间值,比如:dateadd(yy,30,getdate()) ...

  3. 2018.2.2 java中的Date如何获取 年月日时分秒

    package com.util; import java.text.DateFormat; import java.util.Calendar; import java.util.Date; pub ...

  4. JS获取年月日时分秒

    var d = new Date(); ) + "-" + d.getDate() + " " + d.getHours() + ":" + ...

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

    开始使用new Date()测试,并用通过date.getMonth(),和date.getDay()获取,不过后来发现这两个访求是jdk1.1版本的,现在已经不用了,而且结果也不正确. ; int ...

  6. TEST===>Sqlserver中获取年月日时分秒

    可以用两种方法获取 1. select GETDATE() as '当前日期', DateName(year,GetDate()) as '年', DateName(month,GetDate()) ...

  7. scala转换date提取年月日时分秒

    从数据库提取出来的时间为 String 格式,现在需要转换为 date 并提取出里面的 小时 时间段: import java.text.SimpleDateFormat import java.ut ...

  8. java日期格式(年月日时分秒毫秒)

    package test.remote.tools.combine; import java.text.SimpleDateFormat; import java.util.Calendar; imp ...

  9. java Date日期去掉时分秒

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");String s = sdf.format(new Date() ...

随机推荐

  1. There is already an open DataReader associated with this Connection which must be closed first

    使用MVC4 EF Linq获取foreach列表循环的时候遇到了如下的问题:报错提示 There is already an open DataReader associated with this ...

  2. Swift中可选型的Optional Chaining 和 Nil-Coalesce(Swift2.1)

    /* 下面是介绍Optional Chaining 和 Nil-Coalesce */ // Optional Chaining (可选链) if let errorMessage = errorMe ...

  3. Java实现多线程下载

    package cn.test.DownLoad; import java.io.File; import java.io.InputStream; import java.io.RandomAcce ...

  4. explicit 关键字

    C++ explicit关键字用来修饰类的构造函数,表明该构造函数是显式的,既然有"显式"那么必然就有"隐式",那么什么是显示而什么又是隐式的呢? 如果c++类 ...

  5. 信息增益(IG,Information Gain)的理解和计算

    决策树构建中节点的选择靠的就是信息增益了. 信息增益是一种有效的特征选择方法,理解起来很简单:增益嘛,肯定是有无这个特征对分类问题的影响的大小,这个特征存在的话,会对分类系统带来多少信息量,缺了他行不 ...

  6. [BZOJ]3737 [Pa2013]Euler

    从这个FB开始写博客啦. 也不知道会坚持多久…… = =似乎要加一句转载请注明出处 http://www.cnblogs.com/DancingOnTheTree/p/4026076.html htt ...

  7. YOU邮件

    include("class.phpmailer.php");include("class.smtp.php");//获取一个外部文件的内容$mail      ...

  8. nginx配置使其支持thinkphp的pathinfo模式

    #user root;#user nobody;worker_processes 1; #error_log logs/error.log;#error_log logs/error.log noti ...

  9. js 音乐

    define(function(require,exports,module){ var $music = document.getElementById('music'); var $music_m ...

  10. 一个读取propeties配置文件的工具类,线程安全的

    public class ConfigUtil { private static Map<String,Properties> map = new HashMap<String,Pr ...