Java如何mysql 获取当前时间戳戳的读取

请登录查看
Java 实例 - 获取当前时间
以下实例演示了如何使用 Date 类及 SimpleDateFormat 类的 format(date) 方法来输出当前时间:
author by w3cschool.cn
文件名:Main.java
import java.text.SimpleDateF
import java.util.D
public class Main{
public static void main(String[] args){
SimpleDateFormat sdf = new SimpleDateFormat();// 格式化时间
sdf.applyPattern("yyyy-MM-dd HH:mm:ss a");// a为am/pm的标记
Date date = new Date();// 获取当前时间
System.out.println("现在时间:" + sdf.format(date)); // 输出已经格式化的现在时间(24小时制)
以上代码运行输出结果为:
现在时间: 21:27:28 下午
意见反馈:
联系方式:
广告等垃圾信息
不友善内容
违反法律法规的内容
不宜公开讨论的政治内容java获取指定时间的年月日? - 知乎36被浏览<strong class="NumberBoard-itemValue" title="2分享邀请回答import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Demo {
public static void main(String[] args) throws ParseException {
Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(" 20:27:00");
Calendar now = Calendar.getInstance();
now.setTime(date);
int year = now.get(Calendar.YEAR);
int month = now.get(Calendar.MONTH) + 1; // 0-based!
int day = now.get(Calendar.DAY_OF_MONTH);
System.out.println("year: " + year);
System.out.println("month: " + month);
System.out.println("day: " + day);
结果如下:year: 2015
1.2 joda-time (推荐)import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
public class Demo {
public static void main(String[] args) {
DateTimeFormatter formatter = DateTimeFormat.forPattern("YYYY-MM-dd HH:mm:ss");
LocalDate localDate = formatter.parseLocalDate(" 20:27:00");
System.out.println("yearOfCentury: " + localDate.getYearOfCentury());
System.out.println("monthOfYear: " + localDate.getMonthOfYear());
System.out.println("dayOfMonth: " + localDate.getDayOfMonth());
结果如下:yearOfCentury: 15
monthOfYear: 9
dayOfMonth: 17
2. java 8,直接使用 new datetime api (推荐!!)import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Demo {
public static void main(String[] args) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime ldt = LocalDateTime.parse(" 20:27:00", formatter);
System.out.println("Year: " + ldt.getYear());
System.out.println("Month: " + ldt.getMonth().getValue());
System.out.println("DayOfMonth: " + ldt.getDayOfMonth());
结果如下:Year: 2015
DayOfMonth: 17
P.Sjava 8 提供的 datetime api 参考了 jodatime,因此两者看起来很相似,使用 java 8 datetime api 时,如果要将日期时间存储到数据库, 还应考虑数据库驱动是否支持该 apijava 7 及之前版本,推荐使用 jodatime, 省时省力省心我的其他回答:更多回答,请关注我,获取最新动态如果此回答帮到了你,一个小小的赞,一次分享,都会让更多人受益此外,有兴趣的话,欢迎加入我的 Java EE 自学群一号群:(即将满额): (二维码自动识别)二号群:(新群,限时免费加入):
(二维码自动识别)如果觉得我的回答对你很有帮助,可以考虑微信打赏:282 条评论分享收藏感谢收起1添加评论分享收藏感谢收起博客分类:
好几天没写东西了……新年什么的事情特别多 OTL
这几天一直在应付考试。总算都考完了。另外也读了好些以前没接触过的方面的资料,像是Programming Ruby之类。收获不少,不过没时间在这边记录。话说Programming Ruby和Ruby Cookbook的中文版分别是99和98元,现在的技术书真是太贵了 =_=|||
记点小东西。获取带格式的当前日期/时间的方法。C#的ToString()果然还是我觉得最有爱的format方式。不过脚本语言一般也都会提供非常方便的方法,像PHP、Perl和Ruby等等都有相当不错的方法;JavaScript虽然没提供format方法,但自己写一个不废什么事。
我们都知道C/C++里提供了__DATE__和__TIME__宏,不过这两个宏记录的是编译时的日期和时间,而且无法自定义格式,跟这里讨论的可以说是完全没关系……
============================================================
DOS Batch File(on Windows NT):
主要依靠命令行的date /T与time /T命令来分别获取当前系统日期和时间。
例子:
星期六
11:20
============================================================
:
import java.util.D
import java.util.L
import java.text.DateF
public class Greet {
public static void main(String[] args) {
DateFormat df
= DateFormat.getDateTimeInstance(
DateFormat.FULL, DateFormat.SHORT, Locale.CHINA);
String message = df.format(new Date());
System.out.println(message);
// 日 星期五 下午07:54
Java里相关的几个类算是java.util.Date,java.util.Time,java.util.Calendar,java.text.DateFormat,java.text.SimpleDateFormat等。
这里的例子里我没有使用自定义格式,直接用了标准格式中的DateFormat.FULL所指定的格式。
如果使用SimpleDateFormat则可以指定自定义格式的参数:
引用Letter& Date or Time Component& Presentation& Examples&
G& Era designator& Text& AD&
y& Year& Year& 1996; 96&
M& Month in year& Month& J J 07&
w& Week in year& Number& 27&
W& Week in month& Number& 2&
D& Day in year& Number& 189&
d& Day in month& Number& 10&
F& Day of week in month& Number& 2&
E& Day in week& Text& T Tue&
a& Am/pm marker& Text& PM&
H& Hour in day (0-23)& Number& 0&
k& Hour in day (1-24)& Number& 24&
K& Hour in am/pm (0-11)& Number& 0&
h& Hour in am/pm (1-12)& Number& 12&
m& Minute in hour& Number& 30&
s& Second in minute& Number& 55&
S& Millisecond& Number& 978&
z& Time zone& General time zone& Pacific Standard T PST; GMT-08:00&
Z& Time zone& RFC 822 time zone& -0800
============================================================

import java.util.{Date, Locale}
import java.text.DateFormat
import java.text.DateFormat._
object ChinaDate {
def main(args: Array[String]) {
val now = new Date
val df = getDateInstance(LONG, Locale.CHINA)
println(df format now)
Java平台上的一种函数式脚本语言。它可以依靠Java的标准库来完成操作,所以跟Java放在一起来记录。
============================================================
Velocity:
============================================================
C#:
using S
sealed class Greet {
static void Main(string[] args) {
string nowString
= DateTime.Now.ToString("yyyy年MM月dd日 dddd tt hh时mm分");
Console.WriteLine(nowString);
// 日 星期五 下午 08时11分
格式参数可以参考
引用MSDN上的简明表格如下:(
标准格式:
引用Format pattern
Associated Property/Description
ShortDatePattern
D
LongDatePattern
f
Full date and time (long date and short time)
FullDateTimePattern (long date and long time)
General (short date and short time)
General (short date and long time)
MonthDayPattern
o, O
Round-trip date/ with this format pattern, the formatting or parsing operation always uses the invariant culture
r, R
RFC1123P with this format pattern, the formatting or parsing operation always uses the invariant culture
s
SortableDateTimePattern (based on ISO 8601) with this format pattern, the formatting or parsing operation always uses the invariant culture
t
ShortTimePattern
T
LongTimePattern
u
UniversalSortableDateTimePattern using the format for un with this format pattern, the formatting or parsing operation always uses the invariant culture
Full date and time (long date and long time) using universal time
YearMonthPattern
自定义格式:
引用Format pattern
Description
d, %d
The day of the month. Single-digit days do not have a leading zero. The application specifies "%d" if the format pattern is not combined with other format patterns.
dd
The day of the month. Single-digit days have a leading zero.
ddd
The abbreviated name of the day of the week, as defined in AbbreviatedDayNames.
The full name of the day of the week, as defined in DayNames.
The fraction of a second in single-digit precision. The remaining digits are truncated. The application specifies "%f" if the format pattern is not combined with other format patterns.
ff
The fraction of a second in double-digit precision. The remaining digits are truncated.
The fraction of a second in three-digit precision. The remaining digits are truncated.
The fraction of a second in four-digit precision. The remaining digits are truncated.
The fraction of a second in five-digit precision. The remaining digits are truncated.
The fraction of a second in six-digit precision. The remaining digits are truncated.
The fraction of a second in seven-digit precision. The remaining digits are truncated.
Displays the most significant digit of the seconds fraction. Nothing is displayed if the digit is zero. The application specifies "%F" if the format pattern is not combined with other format patterns.
FF
Displays the two most significant digits of the seconds fraction. However, trailing zeros, or two zero digits, are not displayed.
FFF
Displays the three most significant digits of the seconds fraction. However, trailing zeros, or three zero digits, are not displayed.
FFFF
Displays the four most significant digits of the seconds fraction. However, trailing zeros, or four zero digits, are not displayed.
FFFFF
Displays the five most significant digits of the seconds fraction. However, trailing zeros, or five zero digits, are not displayed.
FFFFFF
Displays the six most significant digits of the seconds fraction. However, trailing zeros, or six zero digits, are not displayed.
FFFFFFF
Displays the seven most significant digits of the seconds fraction. However, trailing zeros, or seven zero digits, are not displayed.
gg
The period or era. This pattern is ignored if the date to be formatted does not have an associated period or era string.
The hour in a 12-hour clock. Single-digit hours do not have a leading zero. The application specifies "%h" if the format pattern is not combined with other format patterns.
hh
The hour in a 12-hour clock. Single-digit hours have a leading zero.
The hour in a 24-hour clock. Single-digit hours do not have a leading zero. The application specifies "%H" if the format pattern is not combined with other format patterns.
HH
The hour in a 24-hour clock. Single-digit hours have a leading zero.
Different values of the Kind property, that is, Local, Utc, or Unspecified.
m, %m
The minute. Single-digit minutes do not have a leading zero. The application specifies "%m" if the format pattern is not combined with other format patterns.
mm
The minute. Single-digit minutes have a leading zero.
The numeric month. Single-digit months do not have a leading zero. The application specifies "%M" if the format pattern is not combined with other format patterns.
MM
The numeric month. Single-digit months have a leading zero.
The abbreviated name of the month, as defined in AbbreviatedMonthNames.
The full name of the month, as defined in MonthNames.
The second. Single-digit seconds do not have a leading zero. The application specifies "%s" if the format pattern is not combined with other format patterns.
ss
The second. Single-digit seconds have a leading zero.
The first character in the AM/PM designator defined in AMDesignator or PMDesignator, if any. The application specifies "%t" if the format pattern is not combined with other format patterns.
tt
The AM/PM designator defined in AMDesignator or PMDesignator, if any. Your application should use this format pattern for languages for which it is necessary to maintain the distinction between AM and PM. An example is Japanese, for which the AM and PM designators differ in the second character instead of the first character.
The year without the century. If the year without the century is less than 10, the year is displayed with no leading zero. The application specifies "%y" if the format pattern is not combined with other format patterns.
yy
The year without the century. If the year without the century is less than 10, the year is displayed with a leading zero.
The year in three digits. If the year is less than 100, the year is displayed with a leading zero.
yyyy
The year in four or five digits (depending on the calendar used), including the century. Pads with leading zeros to get four digits. Thai Buddhist and Korean calendars have five-digit years. Users selecting the "yyyy" pattern see all five digits without leading zeros for calendars that have five digits. Exception: the Japanese and Taiwan calendars always behave as if "yy" is selected.
yyyyy
The year in five digits. Pads with leading zeros to get five digits. Exception: the Japanese and Taiwan calendars always behave as if "yy" is selected.
yyyyyy
The year in six digits. Pads with leading zeros to get six digits. Exception: the Japanese and Taiwan calendars always behave as if "yy" is selected. The pattern can be continued with a longer string of "y"s padding with more leading zeros.
z, %z
The time zone offset ("+" or "-" followed by the hour only). Single-digit hours do not have a leading zero. For example, Pacific Standard Time is "-8". The application specifies "%z" if the format pattern is not combined with other format patterns.
zz
The time zone offset ("+" or "-" followed by the hour only). Single-digit hours have a leading zero. For example, Pacific Standard Time is "-08".
The full time zone offset ("+" or "-" followed by the hour and minutes). Single-digit hours and minutes have leading zeros. For example, Pacific Standard Time is "-08:00".
The default time separator defined in TimeSeparator.
The default date separator defined in DateSeparator.
Where c is a format pattern if used alone. To use format pattern "d", "f", "F", "h", "m", "s", "t", "y", "z", "H", or "M" by itself, the application specifies "%d", "%f", "%F", "%h", "%m", "%s", "%t", "%y", "%z", "%H", or "%M".
The "%" character can be omitted if the format pattern is combined with literal characters or other format patterns.
Where c is any character. Displays the character literally. To display the backslash character, the application should use "\\".
C#另外有DateTime.UtcNow,用于获取当前的UTC时间。
虽然是用C#来说,其它在.NET Framework上运行的程序语言当然也能用同样的类库,例如说C++/CLI、VB.NET、JScript.NET等,不重复记录了。
============================================================
JavaScript:
var now = new Date();
var nowString = date.toLocaleString()+' 星期'+'日一二三四五六'.charAt(date.getDay());
print(nowString);
// 日 20:19:48 星期五
JavaScript没有内建的对日期的format方法。通常的做法是自己写一个方法接到Date.prototype.format上,通过使用getYear()、getMonth()、getDay()等方法与正则表达式配合来实现根据format string指定输出的日期格式。
上面的方法是豆腐写的,多谢哦。能用这种简短的方法来写还是多得JavaScript采用UNICODE为内置字符编码。像Ruby就没办法直接用String#[]和fixnum#chr来处理非ASCII字符。
============================================================
: (引用自 )
import std.
void main(char[][] args) {
// Grab the date and time relative to UTC
d_time lNow = std.date.getUTCtime();
// Convert this into the local date and time for display.
char[] lNowString = std.date.toString(lNow);
printf("%.*s", lNowString);
// Fri Jan 04 23:16:58 GMT+
D(Phobos)里的d_time基本上就是用UTC时间的。转换成别的形式时才带上时区等的计算。
不过D的标准库目前对英语以外的语言还是不够友善,可惜。
另外Phobos里这toString()的输出形式固定是"Www Mmm dd hh:mm:ss GMT+-TZ yyyy"。另外的toDateString()是"Www Mmm dd yyyy",而toTimeString()是"hh:mm:ss GMT+-TZ"。
============================================================

&?php echo $showtime=date("Y年n月j日 H时i分A");?&
&!-- 日 11时56分PM --&
格式参数:
引用相关时间参数:
a - "am" 或是 "pm"
A - "AM" 或是 "PM"
d - 几日,二位数字,若不足二位则前面补零; 如: "01" 至 "31"
D - 星期几,三个英文字母; 如: "Fri"
F - 月份,英文全名; 如: "January"
h - 12 小时制的小时; 如: "01" 至 "12"
H - 24 小时制的小时; 如: "00" 至 "23"
g - 12 小时制的小时,不足二位不补零; 如: "1" 至 12"
G - 24 小时制的小时,不足二位不补零; 如: "0" 至 "23"
i - 分钟; 如: "00" 至 "59"
j - 几日,二位数字,若不足二位不补零; 如: "1" 至 "31"
l - 星期几,英文全名; 如: "Friday"
m - 月份,二位数字,若不足二位则在前面补零; 如: "01" 至 "12"
n - 月份,二位数字,若不足二位则不补零; 如: "1" 至 "12"
M - 月份,三个英文字母; 如: "Jan"
s - 秒; 如: "00" 至 "59"
S - 字尾加英文序数,二个英文字母; 如: "th","nd"
t - 指定月份的天数; 如: "28" 至 "31"
U - 总秒数
w - 数字型的星期几,如: "0" (星期日) 至 "6" (星期六)
Y - 年,四位数字; 如: "1999"
y - 年,二位数字; 如: "99"
z - 一年中的第几天; 如: "0" 至 "365"
我不熟悉PHP,无法肯定这东西有没有写错……
============================================================

dayOfWeek = [ "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" ]
t = Time.now
nowString = t.strftime("%Y年%m月%d日 ") && dayOfWeek[t.strftime("%w").to_i] && t.strftime(" %H:%M %p")
puts nowString
# 日 星期六 00:25 AM
引用%a - The abbreviated weekday name (``Sun'')
%A - The full weekday name (``Sunday'')
%b - The abbreviated month name (``Jan'')
%B - The full month name (``January'')
%c - The preferred local date and time representation
%d - Day of the month (01..31)
%H - Hour of the day, 24-hour clock (00..23)
%I - Hour of the day, 12-hour clock (01..12)
%j - Day of the year (001..366)
%m - Month of the year (01..12)
%M - Minute of the hour (00..59)
%p - Meridian indicator (``AM'' or ``PM'')
%S - Second of the minute (00..60)
%U - Week number of the current year, starting with the first Sunday as the first day of the first week (00..53)
%W - Week number of the current year, starting with the first Monday as the first day of the first week (00..53)
%w - Day of the week (Sunday is 0, 0..6)
%x - Preferred representation for the date alone, no time
%X - Preferred representation for the time alone, no date
%y - Year without a century (00..99)
%Y - Year with century
%Z - Time zone name
%% - Literal ``%'' character
Ruby这边基本上就是通过Date和Time来解决这问题。有Date::today和Time::now,另外就是Date::strftime和Time::strftime。不过Ruby在对中文的支持上也还是不够好啊(还是说我没弄清楚该怎么用?),要是能指定Locale就好了。
Time实例里有完整的时间信息,从年份到秒都有记录。而Date只记录到天,并且有更加严格的检查(以及对Gregorian历法的不同支持等)。
============================================================

Perl的话,装上Date::Format会方便很多。以往的做法是通过localtime()获取毫秒的时间,然后通过Date::Format提供的time2str()函数来转换成自定义的格式。不过从Perl6开始提供了date()和utcdate()来获取当前时间。
浏览 13018
RednaxelaFX
浏览: 2310076 次
来自: 海外
allocatestlye1 顺序为
// Fields o ...
ph7.3 写道jdk8下 scanoops 时出现这个问题: ...
&div class=&quote_title ...
对象的大小为什么不固定,还有域的个数与类型等信息不固定。是为啥 ...
写的不错,赞一个
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'JAVA获取当前系统时间System.currentTimeMillis()
System.currentTimeMillis()产生一个当前的毫秒,这个毫秒其实就是自日0时起的毫秒数,Date()其实就是相当于Date(System.currentTimeMillis());因为Date类还有构造Date(long
date),用来计算long秒与日之间的毫秒差。
得到了这个毫秒数,我们自己也可以算起现在的年月日周时,但是这不是我们去计算的,因为有Calendar。Calendar最终出的结果就是年月日周时时区。
System.currentTimeMillis() 获得的是自 00:00:00.000
到当前时刻的时间距离,类型为long
String.valueOf(System.currentTimeMillis()) 这个语句可转为以下的型式:
long ct = System.currentTimeMillis();
String t = String.valueOf(ct);
其实上面的String t就相当于 ct+&&;
只是转为字符串格式
public String refFormatNowDate() {
&#160; Date nowTime = new
Date(System.currentTimeMillis());
&#160; SimpleDateFormat sdFormatter = new
SimpleDateFormat(&yyyy-MM-dd&);
&#160; String retStrFormatNowDate =
sdFormatter.format(nowTime);
&#160; return retStrFormatNowD
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。Java代码中如何获得当前时间_百度知道
Java代码中如何获得当前时间
用Java代码怎么获得系统当前的时间!
我有更好的答案
Date date=new Date();
DateFormat format=new SimpleDateFormat(&yyyy-MM-dd HH:mm:ss&);
String time=format.format(date);
采纳率:48%
来自团队:
java.util.Date date = new Date();
date即是当前时间对象可以用date.getTime() 得到毫秒数
本回答被网友采纳
public String time() {
Date date=new Date();
DateFormat format=new SimpleDateFormat(&yyyy-MM-dd HH:mm:ss&);
String time=format.format(date); }
本回答被网友采纳
Calendar c = Calendar.getInstance();
System.out.println(c.getTime()); //Wed Mar 12 10:11:21 CST 2008
输出这种形式
&/SPAN&&/li&
SimpleDateFormat simpleDateTimeFormat
SimpleDateFormat( & yyyy-MM-dd HH:mm:ss & );
&/SPAN&&/li&
Calendar.getInstance(Locale.CHINESE);
System.out.println(simpleDateTimeFormat.format(c.getTime()));//输出这种形式
&/SPAN&&/li&
String kk_BlacklistVehicleRegister_LARQ_Input = simpleDateTimeFormat.format(c.getTime()) ;
SimpleDateFormat aDateFormat=new SimpleDateFormat(&yyyy年MM月dd 日HH:MM:ss&);System.out.println(aDateFormat.format(new Date()));
java.util.Date nowdate = new java.util.Date();然后如果你想时间的格式和你想用的时间格式一致 那么就要格式化时间了SimpleDateFormat 的包在java.text包下SimpleDateFormat sdf = new SimpleDateFormat(&yyyy-MM-dd HH:mm:ss&) //年月日 时分秒String t = sdf.parse(nowdate);
new java.sql.Date(new java.util.Date().getTime()).toString()
其他4条回答
为您推荐:
其他类似问题
您可能关注的内容
java的相关知识
&#xe675;换一换
回答问题,赢新手礼包&#xe6b9;
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。

我要回帖

更多关于 php获取当前时间戳 的文章

 

随机推荐