-6

I want date without time in java

unix format is

YYYYMMDDHHMMSS.miliseconds

unix string 20170817134131.384

string val = "20170817134131.384";
        if (val != null) {
            SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
            try {
                date = df.parse(String.valueOf(val));
            } catch (ParseException e) {
                throw new RuntimeException("Failed to parse date: ", e);
            }
            return date;
        }
Charles Morrison
  • 418
  • 1
  • 4
  • 23
  • What is `val` ? – Stewart Aug 18 '17 at 18:00
  • yyyyMMddHHmmss **.SSS** maybe, just read the doc –  Aug 18 '17 at 18:02
  • I have no idea what you're asking. – Sotirios Delimanolis Aug 18 '17 at 18:06
  • What do you get? What do you expect? What is your problem? – Heri Aug 18 '17 at 18:07
  • Remove HHMmss from dateformat – Alireza Sharifi Aug 18 '17 at 18:23
  • Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a [mcve]. Use the "edit" link to improve your *question* - do not add more information via comments. Thanks! – GhostCat Aug 18 '17 at 18:24
  • Possible duplicate of [How do I get a Date without time in Java?](https://stackoverflow.com/questions/5050170/how-do-i-get-a-date-without-time-in-java) – Anddo Aug 18 '17 at 18:25

1 Answers1

0

Here's a simple solution to convert unix time to localDate and then you're free to format it as you please:

long time = 1491231860;
final LocalDate localDate = LocalDate.fromMillisSinceEpoch(unixSeconds);
fmatar
  • 3,490
  • 1
  • 15
  • 11
  • (a) What class is `LocalDate`? I see no `fromMillisSinceEpoch` method on [`java.time.LocalDate`](https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html). Nor do I see it on [`org.joda.time.LocalDate`](http://www.joda.org/joda-time/apidocs/org/joda/time/LocalDate.html). (b) This code ignores the crucial issue of time zone in determining a date. This code assumes the date should be framed as UTC, which may or may not be the intent. – Basil Bourque Aug 19 '17 at 01:24