English 中文(简体)
从时间戳中获取日期@ info: whatsthis
原标题:get date from timestamp

我的代码是:

import java.text.*;
import java.util.Date;

public class DateEx {
    public static void main(String[] args) {
        //String valueFromDB = "2012/06/06 00:00:00";

        String valueFromDB = "2012-12-31 00:00:00.0";
        Date d = new Date(valueFromDB);
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
        String dateWithoutTime = sdf.format(d);

        System.out.println("sdf.format(d) " + dateWithoutTime);

    }
}

用于 " 2012/ 06/ 06 00: 00: 00" , 我需要通过 "" 2012-12- 1- 00: 00. 00. 0" 。 它显示的是非法的参数。 可能是因为我在日期中使用了 -" , 或者因为时间戳分数第二秒。 我需要使用 dd- mm- yyyyy 格式的日期 。

最佳回答

试试这个

SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.S");
String valueFromDB = "2012-12-31 00:00:00.0";
Date d1 = sdf1.parse(valueFromDB);
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
String dateWithoutTime = sdf.format(d1);
System.out.println("sdf.format(d) " + dateWithoutTime);
问题回答

这可能是有益的。

long dateTimeStamp = 1487269800;
Timestamp stamp = new Timestamp(dateTimeStamp*1000);
Date changeDate = new Date(stamp.getTime());

产出:

Date :Fri Feb 17 00:00:00 IST 2017

要将字符串解析为日期类型,请使用以下代码:

DateFormat formatter;
Date date;
formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
date = (Date)formatter.parse("2012-12-31 00:00:00");




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签