0

I have the following code. I am passing start = "2016/01/01 23:59:59" and end = "2017/01/01 23:59:59"

func(String start, String end) {

    SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    Date date1 = sdf1.parse(start);
    long startTime = date1.getTime();
    \\System.out.println(startTime);

    SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    Date date2 = sdf2.parse(end);
    long endTime = date2.getTime();
    \\System.out.println(endTime);

}

I am getting the following error

error: unreported exception ParseException; must be caught or declared to be thrown Date date = sdf.parse(time); ^

How do I rectify this ? And also, Why is it showing me this error ?

yahooo
  • 93
  • 7
  • I recommend you avoid the `SimpleDateFormat` class and friends. It is not only long outdated, it is also notoriously troublesome. Today we have so much better in [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). `LocalDateTime.parse(start, DateTimeFormatter.ofPattern("uuuu/MM/dd HH:mm:ss")).atZone(ZonId.systemDefault()).toInstant().toEpochMilli()`. – Ole V.V. Nov 18 '18 at 06:42
  • As Ole V.V. commented, FYI, the terribly troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), `java.util.Calendar`, and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Nov 18 '18 at 17:19
  • Hints: `String::replace( " " , "T" )`, `LocalDateTime.parse`, `LocalDateTime::atZone`, `ZoneId`, `ZonedDateTime::toInstant()`, `Instant::toEpochMilli`. – Basil Bourque Nov 18 '18 at 17:20
  • @OleV.V. May I know the time complexity of using these inbuilt functions ? – yahooo Nov 19 '18 at 18:11
  • I don’t know, @yahooo. Never was an issue to me. I would generally expect them to take constant time. – Ole V.V. Nov 19 '18 at 18:13

1 Answers1

1

The DateFormat#parse() methods throws a ParseException. This means that if something goes wrong when parsing a string into a date, this exception may be thrown.

One fix is to just declare your method to throw this exception, e.g.

public long yourMethod() throws ParseException {
    SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    Date date1 = sdf1.parse(start);
    long startTime = date1.getTime();

    return startTime;
}

Or, also taking the advice from the error message, you could place the code into a try catch block:

public long yourMethod() {
    long startTime = -1L;
    try {
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date1 = sdf1.parse(start);
        startTime = date1.getTime();
    }
    catch (ParseException e) {
        // something went wrong
    }

    return startTime;
}
Tim Biegeleisen
  • 387,723
  • 20
  • 200
  • 263
  • Thank you for the answer. Also, May I know why it is showing me this error? I am passing the date of the specified format. – yahooo Nov 18 '18 at 05:21
  • @yahooo Check my updated answer. – Tim Biegeleisen Nov 18 '18 at 05:33
  • 1
    FYI, the terribly troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Nov 18 '18 at 17:23
  • @BasilBourque I agree completely, but perhaps the OP asked about Java 7 dates because he is bound to use them for some reason (e.g. I myself am currently using Java 6 for a legacy project). – Tim Biegeleisen Nov 18 '18 at 23:54
  • 1
    FYI, nearly all the *java.time* functionality is found in *ThreeTen-Backport* project for Java 6 & 7 with virtually identical API. Further adapted to Android <26 in *ThreeTenABP* project. – Basil Bourque Nov 19 '18 at 00:00
  • No one is going into their legacy code base to introduce the three ten backport project. The OP is not asking about the date API anyway. – Tim Biegeleisen Nov 19 '18 at 00:09