public class Test {
/**
* 英文简写(默认)如:2010-12-01
*/
public static String FORMAT_SHORT = "yyyy-MM-dd";
/**
* 英文全称 如:2010-12-01 23:15:06
*/
public static String FORMAT_LONG = "yyyy-MM-dd HH:mm:ss";
/**
* 精确到毫秒的完整时间 如:yyyy-MM-dd HH:mm:ss.S
*/
public static String FORMAT_FULL = "yyyy-MM-dd HH:mm:ss.S";
/**
* 中文简写 如:2010年12月01日
*/
public static String FORMAT_SHORT_CN = "yyyy年MM月dd";
/**
* 中文全称 如:2010年12月01日 23时15分06秒
*/
public static String FORMAT_LONG_CN = "yyyy年MM月dd日 HH时mm分ss秒";
/**
* 精确到毫秒的完整中文时间
*/
public static String FORMAT_FULL_CN = "yyyy年MM月dd日 HH时mm分ss秒SSS毫秒"; public static void main(String[] args) {
System.out.println(getTimeString());
System.out.println("返回日期年份:"+getYear(new Date()));
System.out.println("返回月份:"+getMonth(new Date()));
System.out.println("返回当天日份"+getDay(new Date()));
System.out.println("返回当天小时"+getHour(new Date()));
System.out.println("返回当天分"+getMinute(new Date()));
System.out.println("返回当天秒"+getSecond(new Date()));
System.out.println("返回当天毫秒"+getMillis(new Date())); } /**
* 获取当前时间
*/
public static String getTimeString() {
SimpleDateFormat df = new SimpleDateFormat(FORMAT_FULL);
Calendar calendar = Calendar.getInstance();
return df.format(calendar.getTime());
} /**
* 获取日期年份
* @param date 日期
* @return
*/
public static String getYear(Date date) {
return format(date).substring(0, 4);
}
/**
* 功能描述:返回月
*
* @param date
* Date 日期
* @return 返回月份
*/
public static int getMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.MONTH) + 1;
} /**
* 功能描述:返回日期
*
* @param date
* Date 日期
* @return 返回日份
*/
public static int getDay(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.DAY_OF_MONTH);
} /**
* 功能描述:返回小时
*
* @param date
* 日期
* @return 返回小时
*/
public static int getHour(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.HOUR_OF_DAY);
} /**
* 功能描述:返回分
*
* @param date
* 日期
* @return 返回分钟
*/
public static int getMinute(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.MINUTE);
} /**
* 返回秒钟
*
* @param date
* Date 日期
* @return 返回秒钟
*/
public static int getSecond(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.SECOND);
} /**
* 功能描述:返回毫
*
* @param date
* 日期
* @return 返回毫
*/
public static long getMillis(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.getTimeInMillis();
}
}

原文:https://blog.csdn.net/xuforeverlove/article/details/81565173

//日期字符串类型转date
public void test() throws ParseException {
String string = "2016-10-24 21:59:06";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.parse(string));
}

【转】java获取当前年、月、日 、小时 、分钟、 秒、 毫秒的更多相关文章

  1. java实现获取当前年月日 小时 分钟 秒 毫秒

    java代码实现如下 view source print?     /**      * 英文简写(默认)如:2010-12-01      */     public static String F ...

  2. Java获取某年某月的第一天

    Java获取某年某月的第一天 1.设计源码 FisrtDayOfMonth.java: /** * @Title:FisrtDayOfMonth.java * @Package:com.you.fre ...

  3. Java获取某年某月的最后一天

    Java获取某年某月的最后一天 1.设计源码 LastDayOfMonth.java: /** * @Title:LastDayOfMonth.java * @Package:com.you.free ...

  4. JavaScript秒转换成天-小时-分钟-秒

    根据时间秒转换成天-小时-分钟-秒 // 秒转换成day.hour.minutes.seconds formatSecond(second: number) { const days = Math.f ...

  5. 【PTA】5-2 下列程序读入时间数值,将其加1秒后输出,时间格式为:hh: mm: ss,即“小时:分钟:秒”,当小时等于24小时,置为0。

    5-2 下列程序读入时间数值,将其加1秒后输出,时间格式为:hh: mm: ss,即"小时:分钟:秒",当小时等于24小时,置为0. #include <stdio.h> ...

  6. Java获取某年某月的第一天和最后一天

    /** * 获取某年某月的第一天 * @Title:getFisrtDayOfMonth * @Description: * @param:@param year * @param:@param mo ...

  7. ASP.NET C# 日期 时间 年 月 日 时 分 秒 格式及转换(转自happymagic的专栏)

    在平时编码中,经常要把日期转换成各种各样的形式输出或保持,今天专门做了个测试,发现DateTime的ToString()方法居然有这么多的表现形式,和大家一起分享. DateTime time=Dat ...

  8. ASP.NET 日期 时间 年 月 日 时 分 秒 格式及转换

    在平时编码中,经常要把日期转换成各种各样的形式输出或保持,今天专门做了个测试,发现DateTime的ToString()方法居然有这么多的表现形式,和大家一起分享. DateTime time=Dat ...

  9. oracle截取时间的年/月/日/时/分/秒

    修改日期格式为年月日时分秒: alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss';select to_char(sysdate,'yyy ...

  10. Java 获取年 月 日 时 分 秒

    /** * 英文简写(默认)如:2010-12-01 */ public static String FORMAT_SHORT = "yyyy-MM-dd"; /** * 英文全称 ...

随机推荐

  1. TestNG-Annotations

    @BeforeSuite The annotated method will be run before all tests in this suite have run.  @AfterSuite ...

  2. Java字节流read函数

    问题引入 做Java作业从标准输入流获取用户输入,用到了System.in.read(),然后出现了bug. //随机生成一个小写字母,用户猜5次,读取用户输入,并判断是否猜对 import java ...

  3. 《wifi加密破解论文》翻译介绍-wifi不再安全

    前言 wifi的加密协议WPA2已经被破解,影响范围包括所有支持wifi的设备,包括Android,Linux,Apple,Windows,OpenBSD,联发科技,Linksys等.其中对Andro ...

  4. git命令行提交流程

    一.顺利提交无冲突情况(diff->add->fetch->pull->commit->push) 1.git  status 查看状态 2. git diff head ...

  5. Mixed Content混合内容错误 Iframe Http页面无法访问

    问题描述 为通过安全测试, 系统升级为https, 后由于新增了接口(页面集成方式, 即第三方系统某一个界面需要嵌入到我们系统的某个页面中) 采用iframe和重定向方式都报同样的错误, 意思就是我们 ...

  6. linux 安装go环境

    https://golang.google.cn/dl/ 进入这个地址选择一个版本下载 wget https://dl.google.com/go/go1.13.4.linux-amd64.tar.g ...

  7. BZOJ 1257 [CQOI2007]余数之和 数学

    都不知道说什么好...咕咕到现在.. 求:$\sum_{i=1}^n \space k\space mod \space i$ 即求:$n*k-\sum_{i=1}^n\space \lfloor \ ...

  8. JVM(十一),垃圾回收之老年代垃圾收集器

    十一.垃圾回收之老年代垃圾收集器 1.Serial Old收集器(标记整理算法-单线程-Client模式下) 2.Paraller Old收集器(标记整理算法-多线程-) 3.CMS收集器(标记清除算 ...

  9. JVM GC之垃圾收集算法

    1.垃圾收集概念 GC目的 分配内存,为每个新建的对象分配空间 确保还在使用的对象的内存一直还在,不能把有用的空间当垃圾回收了 释放不再使用的对象所占用的空间 我们把还被引用的对象称为活的,把不再被引 ...

  10. CISCO实验记录四:备份路由器的IOS

    1.配置好TFTP服务器(假设ip为192.168.2.1) 2.查看当前IOS名称 #show version 输出中有一段:System image file is "bootflash ...