Code666 (代码贴、代码片段)
创建
最近
趋势
关于
[Java] 分别获取当前年月日 小时 分钟 秒 毫秒 →→→→→
进入此内容的聊天室
来自 , 2021-05-01, 写在 Java, 查看 161 次.
URL
http://www.code666.cn/view/1aa48fc4
下载便签
或
查看源码
—
扩张便签
来填满整个浏览器
/**
* 英文简写(默认)如: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
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
.
getInstance
(
)
;
calendar.
setTime
(
date
)
;
return
calendar.
get
(
Calendar
.
MONTH
)
+
1
;
}
/**
* 功能描述:返回日
*
* @param date
* Date 日期
* @return 返回日份
*/
public
static
int
getDay
(
Date
date
)
{
calendar
=
Calendar
.
getInstance
(
)
;
calendar.
setTime
(
date
)
;
return
calendar.
get
(
Calendar
.
DAY_OF_MONTH
)
;
}
/**
* 功能描述:返回小
*
* @param date
* 日期
* @return 返回小时
*/
public
static
int
getHour
(
Date
date
)
{
calendar
=
Calendar
.
getInstance
(
)
;
calendar.
setTime
(
date
)
;
return
calendar.
get
(
Calendar
.
HOUR_OF_DAY
)
;
}
/**
* 功能描述:返回分
*
* @param date
* 日期
* @return 返回分钟
*/
public
static
int
getMinute
(
Date
date
)
{
calendar
=
Calendar
.
getInstance
(
)
;
calendar.
setTime
(
date
)
;
return
calendar.
get
(
Calendar
.
MINUTE
)
;
}
/**
* 返回秒钟
*
* @param date
* Date 日期
* @return 返回秒钟
*/
public
static
int
getSecond
(
Date
date
)
{
calendar
=
Calendar
.
getInstance
(
)
;
calendar.
setTime
(
date
)
;
return
calendar.
get
(
Calendar
.
SECOND
)
;
}
/**
* 功能描述:返回毫
*
* @param date
* 日期
* @return 返回毫
*/
public
static
long
getMillis
(
Date
date
)
{
calendar
=
Calendar
.
getInstance
(
)
;
calendar.
setTime
(
date
)
;
return
calendar.
getTimeInMillis
(
)
;
}
回复 "分别获取当前年月日 小时 分钟 秒 毫秒"
这儿你可以回复上面这条便签
作者
你的名字是?
标题
给你的便签一个标题。
语言
你的便签是以
Plain Text
HTML5
CSS
JavaScript
PHP
Python
Ruby
Lua
Bash
Erlang
Go
C
C++
Diff-output
LaTeX
SQL
XML
-----------------
4CS
MOS 6502
MOS 6502 Kick Assembler
MOS 6502 TASM/64TASS
Motorola 68000 Devpac Assembler
ABAP
Actionscript
ActionScript3
Ada
AIMMS
ALGOL 68
Apache
AppleScript
Apt sources.list
ARM Assembler
x86 Assembler
asymptote
ASP
autoconf
Autohotkey
AutoIT
AviSynth
Awk
BASCOM AVR
Basic4GL
BBCode
Brainfuck
BibTeX
BlitzBasic
BNF (Backus-Naur form)
Boo
C (for LoadRunner)
C for Macs
C with WiAPI
CAD DCL (Dialog Control Language)
AutoCAD/IntelliCAD Lisp
CFDG
ColdFusion
ChaiScript
Chapel
CIL (Common Intermediate Language)
Clojure
CMake
COBOL
CoffeeScript
C++ with WinAPI
C#
Cuesheet
D
Dart
DCS
DCL
DCPU/16 Assembly
Delphi (Object Pascal)
DIV
DOS
dot
E
ECMAScript
Eiffel
Email (mbox/eml/RFC format)
Enerscript
Euphoria
EZT
Formula One
Falcon
fo
Fortran
FreeBasic
FreeSWITCH
F#
GAMBAS
GDB
Genero
Genie
GNU Gettext .po/.pot
glSlang
GML
Gnuplot script
Groovy
GwBasic
Haskell
Haxe
HicEst
HQ9+
HTML 4.01 strict
Icon
Unoidl
INI
Inno Script
INTERCAL
Io
ISPF Panel
J
Java
Java 5
Job Control Language
jQuery 1.3
KLone with C
KLone with C++
Kotlin
Liberty BASIC
LDIF
Generic Lisp
LLVM
Locomotive Basic (Amstrad CPC series)
Logcat
Logtalk
LOLcode
@Formula/@Command
LotusScript
Lightwave Script
Linden Scripting
Motorola 68000 Assembler
MagikSF
Make
MapBasic
Matlab M-file
mIRC Scripting
MMIX Assembler
Modula-2
Modula-3
Microchip Assembler
MXML
MySQL
Nagios
NetRexx
newLISP
nginx
Nimrod
Nullsoft Scriptable Install System
Oberon-2
Objective-C
Objeck Programming Language
OCaml (Objective Caml)
GNU Octave M-file
OpenOffice.org Basic
ooRexx
Oracle 11i
Oracle 8
Delphi Prism (Oxygene)
Oz
ParaSail
PARI/GP
Pascal
PCRE
Per (forms)
Perl
Perl 6
OpenBSD packet filter
PIC16 Assembler
Pike
Pixel Bender 1.0
PL/I
Oracle 9.2 PL/SQL
PostgreSQL
Postscript
Povray
PowerBuilder (PowerScript)
PowerShell
ProFTPd
Progress
Prolog
Property
ProvideX
PureBasic
Python for S60
q/kdb+
QBasic/QuickBASIC
QML
Racket
Ruby (with Ruby on Rails Framework)
RBS Script
Rebol
Microsoft Registry Editor
Rexx
robots.txt
RPM Spec
R
Rust
SAS
Scala
Scheme
SciLab
SCL
sdlBasic
Smalltalk
Smarty template
SPARK
SPARQL
StandardML
StoneScript
SystemVerilog IEEE 1800-2009(draft8)
TCL/iTCL
Tera Term Macro
thinBasic
T-SQL
TypoScript
Unicon
UnrealScript
UPC
Urbi
Vala
Visual Basic
VB.NET
VBScript
Vedit macro language
Verilog
VHDL
Vim scripting
Visual FoxPro
Visual Prolog
Whitespace
Whois response (RPSL format)
WinBatch
XBasic
xorg.conf
Axapta/Dynamics Ax X++
YAML
ZiLOG Z80 Assembler
ZXBasic
你的便签
在这儿输入便签内容
/** * 英文简写(默认)如: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 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.getInstance(); calendar.setTime(date); return calendar.get(Calendar.MONTH) + 1; } /** * 功能描述:返回日 * * @param date * Date 日期 * @return 返回日份 */ public static int getDay(Date date) { calendar = Calendar.getInstance(); calendar.setTime(date); return calendar.get(Calendar.DAY_OF_MONTH); } /** * 功能描述:返回小 * * @param date * 日期 * @return 返回小时 */ public static int getHour(Date date) { calendar = Calendar.getInstance(); calendar.setTime(date); return calendar.get(Calendar.HOUR_OF_DAY); } /** * 功能描述:返回分 * * @param date * 日期 * @return 返回分钟 */ public static int getMinute(Date date) { calendar = Calendar.getInstance(); calendar.setTime(date); return calendar.get(Calendar.MINUTE); } /** * 返回秒钟 * * @param date * Date 日期 * @return 返回秒钟 */ public static int getSecond(Date date) { calendar = Calendar.getInstance(); calendar.setTime(date); return calendar.get(Calendar.SECOND); } /** * 功能描述:返回毫 * * @param date * 日期 * @return 返回毫 */ public static long getMillis(Date date) { calendar = Calendar.getInstance(); calendar.setTime(date); return calendar.getTimeInMillis(); }
创建短链接
创建一个较短的URL,连接到这个便签
私人
私人便签不会显示在最近列表中
保存期限
我们应该什么时候删除这张便签?
阅后即焚
五分钟
一小时
一天
一周
一月
一年
永久保留
防滥用
键入这些字符
创建