简体   繁体   English

如何将mili第二种格式更改为yyyy-mm-dd hh:mm:ss以插入到具有datetype colunm的mysql中

[英]How to change mili second format to yyyy-mm-dd hh:mm:ss to insert to mysql with datetype colunm

I want to insert date and time to post_date column in Wordpress. 我想在Wordpress的post_date列中插入日期和时间。 The column data type is datetime yyyy-mm-dd hh:mm:ss . 列数据类型为datetime yyyy-mm-dd hh:mm:ss I want to generate first row is: 2015-01-01 01:01:01 and add 60 seconds to the next row 2015-01-01 01:02:01. 我要生成的第一行是:2015-01-01 01:01:01,并将60秒添加到下一行2015-01-01 01:02:01。

I found this code but I don't know how to convert millisecond to second format. 我找到了这段代码,但是我不知道如何将毫秒转换为第二种格式。 It's print 2015-11-22 15:41:29.496, but how to get rid of ".496"? 它是打印2015-11-22 15:41:29.496,但如何摆脱“ .496”? Is my data type when I insert to MySQL was formatted like: datetime? 我插入MySQL时的数据类型是否格式化为:datetime? will wordpress understand it? WordPress会理解吗?

import java.util.*;
import java.sql.*;

long retryDate = System.currentTimeMillis();

int sec = 60;

Timestamp original = new Timestamp(retryDate);
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(original.getTime());
cal.add(Calendar.SECOND, sec);
Timestamp later = new Timestamp(cal.getTime().getTime());

System.out.println(original);
System.out.println(later);

Use cal.set(Calendar.MILLISECOND, 0) to clear the milliseconds. 使用cal.set(Calendar.MILLISECOND, 0)清除毫秒。
Use new Timestamp(cal.getTimeInMillis()) to get as Timestamp . 使用new Timestamp(cal.getTimeInMillis())作为Timestamp获取。

final int sec = 60;

Calendar cal = Calendar.getInstance();
cal.set(Calendar.MILLISECOND, 0);
Timestamp original = new Timestamp(cal.getTimeInMillis());

cal.add(Calendar.SECOND, sec);
Timestamp later = new Timestamp(cal.getTimeInMillis());

System.out.println(original);
System.out.println(later);

Java 8 version using Instant : 使用Instant Java 8版本:

final int sec = 60;

Instant instant = Instant.now().truncatedTo(ChronoUnit.SECONDS);
Timestamp original = Timestamp.from(instant);

instant = instant.plusSeconds(sec);
Timestamp later = Timestamp.from(instant);

System.out.println(original);
System.out.println(later);

Use formatter to format datetime 使用格式化程序格式化日期时间

public static String getCurrentTimeStamp() {
    SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date now = new Date();
    String strDate = sdfDate.format(now);
    return strDate;
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将日期和时间格式从“ yyyy-MM-dd HH:mm:ss”更改为“ h:mm a”? - How to change date and time format from“yyyy-MM-dd HH:mm:ss” to “h:mm a”? Joda DateTime格式为yyyy-MM-DD HH:MM:SS - Joda DateTime format to yyyy-MM-DD HH:MM:SS YYYY-MM-DD HH:MM:SS格式的时间值 - Time value in YYYY-MM-DD HH:MM:SS format Java 格式 yyyy-MM-dd'T'HH:mm:ss.SSSz 到 yyyy-mm-dd HH:mm:ss - Java format yyyy-MM-dd'T'HH:mm:ss.SSSz to yyyy-mm-dd HH:mm:ss 如何比较 java 格式“EEE MMM dd HH:mm:ss zzz yyyy”和“yyyy-MM-dd hh:mm:sss”的日期时间? - How to compare the datetime of format "EEE MMM dd HH:mm:ss zzz yyyy" and "yyyy-MM-dd hh:mm:sss" in java? 如何解析 yyyy-MM-dd HH:mm:ss 到日期? - How to parse a yyyy-MM-dd HH:mm:ss to Date? 如何验证时间戳记(yyyy-MM-dd HH:mm:ss)和(yyyy-MM-dd) - how to validate the timestamp (yyyy-MM-dd HH:mm:ss) and (yyyy-MM-dd) YYYY-MM-DD HH:MM:SS[.fraction] 格式的 MySQL 更新日期字段 - MySQL update date field in YYYY-MM-DD HH:MM:SS[.fraction] format 如何将 java.sql.Timestamp(yyyy-MM-dd HH:mm:ss.S) 格式化为日期(yyyy-MM-dd HH:mm:ss) - How to format a java.sql.Timestamp(yyyy-MM-dd HH:mm:ss.S) to a date(yyyy-MM-dd HH:mm:ss) 将MYSQL日期时间值转换为UTC时区格式yyyy-MM-dd'T'HH:mm:ss'Z' - convert MYSQL datetime value to UTC Timezone format yyyy-MM-dd'T'HH:mm:ss'Z'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM