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. 关于HttpServlet和Servlet以及doPost和doGet关系

    这两天在看Servlet和Jsp,spring太难了,还是先看看基础,只怪自己太弱了. Servlet是一个接口,本身定义的是一种网络服务,HttpServlet是已经实现了Servlet接口,也就是 ...

  2. 单篇文章JS模拟分页

    废话部分 前两天做了一个前台分页插件,支持ajax读取数据绑定前台 和 url带页码参数跳转两种方式.于是稍加改动,做了一个单篇文章js模拟分页的代码,为什么说是模拟分页呢?因为在服务器响应HTML请 ...

  3. 关于Adobe Flash 11.3 引起的火狐使用问题

    Adobe Flash 更新到11.3之后,为火狐引入Flash沙盒安全模式,但同时,又造成了部分兼容性问题,导致 Windows vista及 Windows 7上部分火狐崩溃,并致使一些使用Fla ...

  4. iOS 网络与多线程--7.Performselector消息处理方法

    创建一个IOSApp类 IOSApp.h文件 #import <Foundation/Foundation.h> @interface IOSApp : NSObject // 1.添加一 ...

  5. Caused by: org.apache.ibatis.ognl.OgnlException: source is null for getProperty(null, "mil_id")

    今天在使用mybatis处理数据库的时候,突然抛出了上述异常,让我感到很惊讶,因为在处理save的时候,在Mybatis的配置文件中,我根本就没有使用到ognl表达式,系统怎么会抛出上述异常.而且之前 ...

  6. 在MyEclipse中设置Source folders和output folder

    在一个项目中可能会有多个资源文件,它们共同编译输出到输出文件.那么除了默认的src以外,如何把其他文件设置成资源文件(Source folders)呢?

  7. vim学习心得(一)——Cygwin下vim配置

    关于Vi有很多传说.其中最为著名的是: “Vi是编辑器之神,Emacs是神的编辑器” Emacs没有用过,但是Vi在Linux经常使用,所以,掌握好vi非常重要!!! Vim(Vi Improved) ...

  8. Memcache的基本应用

    $mc = new Memcache(); $mc->connect('127.0.0.1', 11211); $sql = "select * from user where id= ...

  9. php resizeimage 部分jpg文件 生成缩略图失败

    今天遇到GD的resizeimage 函数处理jpg后缀文件的缩略图的时候 提示该图片不是合法的jpg图片并报错 <b>Warning</b>: imagecreatefrom ...

  10. javascript 网页图标音乐切换

    图片名称 sprite.zip <!doctype html> <html> <head> </head> <style> .css{ po ...