Java YYYY-MM-DD HH:MM:SS格式的时间值

Java YYYY-MM-DD HH:MM:SS格式的时间值,java,hibernate,datetime,Java,Hibernate,Datetime,使用hibernate从数据库中获取值 DB将以下时间值数据作为日期时间列(2016-04-11 23:54:11) Hibernate有以下数据 <property name="timevalue" type="timestamp"> <column name="timevalue" length="100" /> </property> @Temporal(TemporalType.TIMESTAMP) private java.util.Dat

使用hibernate从数据库中获取值

DB将以下时间值数据作为日期时间列(2016-04-11 23:54:11)

Hibernate有以下数据

<property name="timevalue" type="timestamp">
    <column name="timevalue" length="100" />
</property>
@Temporal(TemporalType.TIMESTAMP)
private java.util.Date timevalue;
但当我尝试获取数据时,它返回为
timevalue

1460399054000

但是我需要时间值为
YYYY-MM-DD HH:MM:SS


我现在应该怎么做才能修复它呢?

假设您获得数据的时间很长,那么您可以执行以下操作:

long timeStamp = 1460399054000L; // what you wot from web service/DB
String format = "YYYY-MM-DD HH:mm:SS";
Date date = new Date(timeStamp);
String dateFormat =  new SimpleDateFormat(format).format(date);
System.out.println(dateFormat);
这会打印出来

2016-04-102 20:04:00


假设您长时间获得数据,那么您可以执行以下操作:

long timeStamp = 1460399054000L; // what you wot from web service/DB
String format = "YYYY-MM-DD HH:mm:SS";
Date date = new Date(timeStamp);
String dateFormat =  new SimpleDateFormat(format).format(date);
System.out.println(dateFormat);
这会打印出来

2016-04-102 20:04:00


你可以试试乔达图书馆

    long geTime= your value;
    GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone("US/Central"));
    calendar.setTimeInMillis(geTime);
    DateTime jodaTime = new DateTime(geTime, 
    DateTimeZone.forTimeZone(TimeZone.getTimeZone("US/Central")));
    DateTimeFormatter parser1 = DateTimeFormat.forPattern("YYYY-MM-dd HH:mm:ss");
    System.out.println("Get Time : "+parser1.print(jodaTime));

你可以试试乔达图书馆

    long geTime= your value;
    GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone("US/Central"));
    calendar.setTimeInMillis(geTime);
    DateTime jodaTime = new DateTime(geTime, 
    DateTimeZone.forTimeZone(TimeZone.getTimeZone("US/Central")));
    DateTimeFormatter parser1 = DateTimeFormat.forPattern("YYYY-MM-dd HH:mm:ss");
    System.out.println("Get Time : "+parser1.print(jodaTime));

我应该在哪里实现这个?我想Hibernate会注意的,请更正这是我使用的方法Public List readAll(){Session Session=HibernateUtil.currentSession();List results=Session.createQuery(“来自问题”).List();//results.Session.close();return results;}否,不要使用那种格式
YYYY-MM-DD HH:MM:SS
MM
始终是月份,即使您将其夹在小时和秒之间。而
DD
是一年中的一天,这就是为什么你会得到102。而
yyy
是一年中的一周,在1月的前几天或12月的最后几天可能会很奇怪。而
SS
是毫秒,而不是秒。请改为尝试
yyyy-MM-dd HH:MM:ss
。我应该在哪里实现这一点?我想Hibernate会注意的,请更正这是我使用的公共列表readAll()方法{Session Session=HibernateUtil.currentSession();List results=Session.createQuery(“来自问题”).List();//results.Session.close();返回结果;}不,不要使用那种格式
YYYY-MM-DD HH:MM:SS
MM
始终是月份,即使您将其夹在小时和秒之间。而
DD
是一年中的一天,这就是为什么你会得到102。而
yyy
是一年中的一周,在1月的前几天或12月的最后几天可能会很奇怪。而
SS
是毫秒,而不是秒。改为尝试
yyyy-MM-dd HH:MM:ss
。如何打印或查看
timevalue
?通过REST获取{“id”:2,“问题”:“你叫什么名字”,“noans”:1,“用户名”:“Harshitha 1”,“timevalue”:1460399054000},使用SimpleDateFormat作为@xoce-웃-Пepeúpa建议如下或类似于JodaTime的东西。
1460399054000
输出是您环境中的默认格式,您需要根据API公开的格式或向用户显示的格式对其进行显式格式化。您如何打印或查看
timevalue
?通过REST获取{“id”:2,“问题”:“您叫什么名字”,“noans”:1,“用户名”:“Harshitha 1”,“时间值”:1460399054000},使用SimpleDataFormat作为@xoce-웃-Пepeúpa建议如下或类似于JodaTime的东西。
1460399054000
输出是您环境中的默认格式,您需要根据API公开的格式或向用户显示的格式对其进行显式格式化。