简体   繁体   中英

Time value in YYYY-MM-DD HH:MM:SS format

Using hibernate for fetching the values from DB.

DB has below data for timevalue as datetime column(2016-04-11 23:54:11)

Hibernate has below data

<property name="timevalue" type="timestamp">
    <column name="timevalue" length="100" />
</property>

I have declared as below data

@Temporal(TemporalType.TIMESTAMP)
private java.util.Date timevalue;

But when I tried fetching the data it is returned as timevalue :

1460399054000

But I need the timevalue as YYYY-MM-DD HH:MM:SS

What should I do now for fixing it?

Assuming that you get the data as a long then you can do:

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);

this will print

2016-04-102 20:04:00

You can try with Joda Library

    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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM