1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.jboss.as.quickstarts.loggingToolsQS;
18
19 import java.text.DateFormat;
20 import java.text.ParseException;
21 import java.text.SimpleDateFormat;
22 import java.util.Date;
23
24 import javax.ws.rs.GET;
25 import javax.ws.rs.Path;
26 import javax.ws.rs.PathParam;
27 import javax.ws.rs.WebApplicationException;
28 import javax.ws.rs.core.MediaType;
29 import javax.ws.rs.core.Response;
30
31 import org.jboss.as.quickstarts.loggingToolsQS.exceptions.DateExceptionsBundle;
32 import org.jboss.as.quickstarts.loggingToolsQS.loggers.DateLogger;
33
34
35
36
37
38
39
40
41 @Path("dates")
42 public class DateService {
43
44 @GET
45 @Path("daysuntil/{targetdate}")
46 public int showDaysUntil(@PathParam("targetdate") String targetdate) {
47 DateLogger.LOGGER.logDaysUntilRequest(targetdate);
48
49 DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
50 Date target = null;
51 Date now = new Date();
52
53 float days = 0;
54
55 try {
56 df.setLenient(false);
57 target = df.parse(targetdate);
58 days = (float) target.getTime() - now.getTime();
59 days = days / (1000 * 60 * 60 * 24);
60 } catch (ParseException ex) {
61
62
63
64
65
66
67 ParseException nex = DateExceptionsBundle.EXCEPTIONS.targetDateStringDidntParse(targetdate, ex.getErrorOffset());
68
69
70 DateLogger.LOGGER.logStringCouldntParseAsDate(targetdate, nex);
71
72
73 throw new WebApplicationException(Response.status(400).entity(nex.getLocalizedMessage()).type(MediaType.TEXT_PLAIN)
74 .build());
75 }
76
77 return Math.round(days);
78 }
79
80 }