Example usage for java.text DateFormat parse

List of usage examples for java.text DateFormat parse

Introduction

In this page you can find the example usage for java.text DateFormat parse.

Prototype

public Date parse(String source) throws ParseException 

Source Link

Document

Parses text from the beginning of the given string to produce a date.

Usage

From source file:org.jfree.chart.demo.TimePeriodValuesDemo3.java

/**
 * Creates a dataset, consisting of two series of monthly data.
 *
 * @return the dataset./*from w  w w .j  a v  a  2  s  . c o  m*/
 */
public XYDataset createDataset() {

    final TimePeriodValues s1 = new TimePeriodValues("Series 1");

    final DateFormat df = DateFormat.getInstance();
    try {
        final Date d0 = df.parse("11/5/2003 0:00:00.000");
        final Date d1 = df.parse("11/5/2003 0:15:00.000");
        final Date d2 = df.parse("11/5/2003 0:30:00.000");
        final Date d3 = df.parse("11/5/2003 0:45:00.000");
        final Date d4 = df.parse("11/5/2003 1:00:00.001");
        final Date d5 = df.parse("11/5/2003 1:14:59.999");
        final Date d6 = df.parse("11/5/2003 1:30:00.000");
        final Date d7 = df.parse("11/5/2003 1:45:00.000");
        final Date d8 = df.parse("11/5/2003 2:00:00.000");
        final Date d9 = df.parse("11/5/2003 2:15:00.000");

        s1.add(new SimpleTimePeriod(d0, d1), 0.39);
        //s1.add(new SimpleTimePeriod(d1, d2), 0.338);
        s1.add(new SimpleTimePeriod(d2, d3), 0.225);
        s1.add(new SimpleTimePeriod(d3, d4), 0.235);
        s1.add(new SimpleTimePeriod(d4, d5), 0.238);
        s1.add(new SimpleTimePeriod(d5, d6), 0.236);
        s1.add(new SimpleTimePeriod(d6, d7), 0.25);
        s1.add(new SimpleTimePeriod(d7, d8), 0.238);
        s1.add(new SimpleTimePeriod(d8, d9), 0.215);
    } catch (Exception e) {
        System.out.println(e.toString());
    }

    final TimePeriodValuesCollection dataset = new TimePeriodValuesCollection();
    dataset.addSeries(s1);
    dataset.setDomainIsPointsInTime(false);

    return dataset;

}

From source file:com.netcracker.financeapp.controller.spending.SpendingServlet.java

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    int value = Integer.parseInt(request.getParameter("value"));

    String from = request.getParameter("fromListVal");
    BankCard currentBankCard = bankCardService.getBankCardByNumber(from);
    if (currentBankCard.getAmount() > value) {
        bankCardService.editCardAmount(currentBankCard.getIdCard(), currentBankCard.getAmount() - value);

        String to = request.getParameter("toListVal");
        Agent currentAgent = agentService.getAgentByName(to);

        Date date = null;/* www .j a v a 2s  .com*/
        try {
            DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
            date = dateFormat.parse(request.getParameter("date"));
        } catch (ParseException ex) {
            Logger.getLogger(SpendingServlet.class.getName()).log(Level.SEVERE, null, ex);
        }

        String description = request.getParameter("description");
        String typeName = request.getParameter("spendingType");

        int typeId = typeService.getTypeByName(typeName).getIdType();
        int transactionTypeId = typeService.getTypeByName(typeName).getIdParent();
        int stateTypeId = typeService.getTypeByName("COMMITED").getIdType();

        int spendingId = spendingService.insertSpending(value, description, date, typeId);
        if (spendingId > 0) {
            request.getRequestDispatcher("templates/success.jsp").forward(request, response);
            transactionService.insertTransaction(transactionTypeId, stateTypeId, currentAgent.getIdAgent(),
                    currentBankCard.getIdCard(), spendingId);

        } else {
            request.getRequestDispatcher("templates/error.jsp").forward(request, response);
        }
    } else {
        request.getRequestDispatcher("templates/error.jsp").forward(request, response);
    }
}

From source file:org.agorava.twitter.jackson.TweetDeserializer.java

private Date toDate(String dateString, DateFormat dateFormat) {
    if (dateString == null) {
        return null;
    }//  w w w .ja v a  2 s .c om

    try {
        return dateFormat.parse(dateString);
    } catch (ParseException e) {
        return null;
    }
}

From source file:wtw.ui.GetChartAction.java

@Override
public String execute() throws Exception {
    Object start = ActionContext.getContext().getSession().get("start");
    Object end = ActionContext.getContext().getSession().get("end");
    if (start != null && end != null) {
        startdate = (String) start;
        enddate = (String) end;/*from   ww  w .  j  a v  a2  s .co  m*/
    }
    if (start == null || end == null) {
        all = 6;
        mobile = 1;
        web = 1;
        software = 1;
        design = 1;
        data = 1;
        other = 1;
    } else {
        Date date2;
        Date date;
        DateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
        date = (Date) formater.parse(startdate);
        date2 = (Date) formater.parse(enddate);
        all = projectManager.getCountProjectInMonth(date, date2);
        mobile = projectManager.getCountProjectInMonthAndCategory(date, date2, " Mobile");
        web = projectManager.getCountProjectInMonthAndCategory(date, date2, " Website");
        software = projectManager.getCountProjectInMonthAndCategory(date, date2, " Software");
        design = projectManager.getCountProjectInMonthAndCategory(date, date2, " Design");
        data = projectManager.getCountProjectInMonthAndCategory(date, date2, " Data Entry");
        other = projectManager.getCountProjectInMonthAndCategory(date, date2, " Other");
    }
    DefaultCategoryDataset dataSet = new DefaultCategoryDataset();
    dataSet.setValue(all, "Population", "Total");
    dataSet.setValue(mobile, "Population", "Mobile");
    dataSet.setValue(web, "Population", "Website");
    dataSet.setValue(software, "Population", "Software");
    dataSet.setValue(data, "Population", "Data Entry");
    dataSet.setValue(design, "Population", "Design");
    dataSet.setValue(other, "Population", "Other");

    chart = ChartFactory.createBarChart3D("Report Quantity Project By Category", "Project Category",
            "Quantity of Project", dataSet, PlotOrientation.VERTICAL, false, true, false);
    return SUCCESS;
}

From source file:com.lohika.alp.reporter.fe.controller.TestController.java

@RequestMapping(method = RequestMethod.GET, value = "/results/test", headers = Headers.ACCEPT_HTML)
public String getTest(@CookieValue(value = FROM, required = false) String from,
        @CookieValue(value = TILL, required = false) String till, Model model,
        @ModelAttribute("testFilter") TestFilter filter) throws Exception {

    setDefaultPeriod(filter);// w  w  w.java 2 s  .co m

    // set times from the cookies
    if (from != null && till != null) {
        DateFormat formatter = new SimpleDateFormat("yy-MM-dd");
        Date f = (Date) formatter.parse(from);
        Date t = (Date) formatter.parse(till);
        filter.setFrom(f);
        filter.setTill(t);
    }

    List<Test> list = testDAO.listTest(filter);
    Map<Test, TestSummary> map = testDAO.getTestSummaryMap(list);

    model.addAttribute("tests", list);
    model.addAttribute("summaryMap", map);
    model.addAttribute("testFilter", filter);

    return "test";
}

From source file:com.autentia.intra.converter.Date2YearConverter.java

/**
 * Devuelve la representacin Date de JAVA de un valor
 *//*w  w  w .  j  a  v  a2 s  .c om*/
public Object getAsObject(FacesContext context, UIComponent component, String value) throws ConverterException {
    DateFormat fmt = new SimpleDateFormat("dd/MM/yyyy");
    Date fecha = null;

    if (!StringUtils.isEmpty(value)) {
        try {
            fecha = fmt.parse(value);
        } catch (Exception pe) {
            String mensaje = "El valor no se corresponde con una fecha vlida";
            throw new ConverterException(new FacesMessage(mensaje));
        }
    }
    return fecha;
}

From source file:itdelatrisu.opsu.downloads.servers.BloodcatServer.java

/**
 * Returns a formatted date string from a raw date.
 * @param s the raw date string (e.g. "2015-09-30 09:39:04.536")
 * @return the formatted date, or the raw string if it could not be parsed
 *//*from   ww w  . j a  v  a  2s  . c  om*/
private String formatDate(String s) {
    try {
        // old format: "2015-05-14T23:38:47+09:00"
        // make string parseable by SimpleDateFormat
        //         int index = s.lastIndexOf(':');
        //         if (index == -1)
        //            return s;
        //         s = new StringBuilder(s).deleteCharAt(index).toString();

        DateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); //"yyyy-MM-dd'T'HH:mm:ssZ"
        Date d = f.parse(s);
        DateFormat fmt = new SimpleDateFormat("d MMM yyyy HH:mm:ss");
        return fmt.format(d);
    } catch (StringIndexOutOfBoundsException | ParseException e) {
        return s;
    }
}

From source file:com.exploringspatial.dao.text.ConflictDaoTest.java

@Test
public void testGet() throws ParseException {
    conflictDao.reloadTableFromCsv(eventsCsvFile);
    final List<Conflict> conflicts = conflictDao.listAll();
    final Conflict conflict = conflicts.get(0);
    Conflict match = conflictDao.get(conflict.getEventPk());
    assertNotNull(match);/*from  ww  w . j  a  va2s .c  o  m*/
    assertEquals(new Integer(615), match.getGwno());
    assertEquals("1ALG", match.getEventIdCountry());
    assertEquals(new Long(1), match.getEventPk());
    final DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
    assertEquals(df.parse("02/01/1997"), match.getEventDate());
    assertEquals(new Integer(1997), match.getYear());
    assertEquals(new Integer(1), match.getTimePrecision());
    assertEquals("Violence against civilians", match.getEventType());
    assertEquals("GIA: Armed Islamic Group", match.getActor1());
    assertEquals(null, match.getAllyActor1());
    assertEquals(new Integer(2), match.getInter1());
    assertEquals("Civilians (Algeria)", match.getActor2());
    assertEquals(null, match.getAllyActor2());
    assertEquals(new Integer(7), match.getInter2());
    assertEquals(new Integer(27), match.getInteraction());
    assertEquals("Algeria", match.getCountry());
    assertEquals("Blida", match.getAdmin1());
    assertEquals("Blida", match.getAdmin2());
    assertEquals(null, match.getAdmin3());
    assertEquals("Blida", match.getLocation());
    assertEquals(36.4686D, match.getLatitude());
    assertEquals(2.8289, match.getLongitude());
    assertEquals("www.algeria-watch.org", match.getSource());
    assertEquals(
            "4 January: 16 citizens were murdered in the village of Benachour (Blida) by masked men a few hundred meters away from a military camp. It is of note that the citizens of this village had refused to set up armed militias.",
            conflict.getNotes());
}

From source file:org.openmrs.module.patientportaltoolkit.fragment.controller.AppointmentsFragmentController.java

public void markScheduled(FragmentModel model,
        @RequestParam(value = "reminderId", required = true) String reminderId,
        @RequestParam(value = "markScheduledDate", required = true) String markScheduledDate,
        PageRequest pageRequest) {/*from  w  w  w .  j  a va  2 s .c o m*/

    Person person = Context.getPersonService().getPerson(
            Context.getService(ReminderService.class).getRemindersById(reminderId).getPatient().getPersonId());
    log.info(PPTLogAppender.appendLog("MARK_SCHEDULED_APPOINTMENTS", pageRequest.getRequest()));
    //log.info("~MARK_SCHEDULED_APPOINTMENTS~" + Context.getAuthenticatedUser().getUsername()+ "~REQ_FOR:"+ Context.getUserService().getUsersByPerson(person,false).get(0).getUsername());
    //System.out.println("121212121212"+markCompletedDate);
    DateFormat format = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH);
    Date date = new Date();
    try {
        date = format.parse(markScheduledDate);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    Context.getService(ReminderService.class).markScheduledReminder(reminderId, date);

}

From source file:com.dougchimento.at.AtManagerSimpleTest.java

@Test
public void testGetJobById() throws Exception {
    AtJob job = atManager.getJobById(1);
    assertNotNull(job);/*w w  w. j  ava2 s. c o m*/
    assertEquals(job.getId(), 1);
    assertEquals(job.getQueue(), "q");
    assertEquals(job.getScript(), "echo this is a test\n");

    DateFormat dateFormatter = new SimpleDateFormat("yyyyMMddHHmm");
    assertEquals(job.getJobDate(), dateFormatter.parse("202001281600"));
}