Example usage for java.text DateFormat format

List of usage examples for java.text DateFormat format

Introduction

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

Prototype

public final String format(Date date) 

Source Link

Document

Formats a Date into a date-time string.

Usage

From source file:com.jslsolucoes.tagria.lib.util.TagUtil.java

public static String format(String type, String value, JspContext jspContext) {

    if (StringUtils.isEmpty(value)) {
        return value;
    } else if ("date".equals(type) || "timestamp".equals(type) || "hour".equals(type)) {

        DateFormat dateFormat = DateFormat.getDateTimeInstance();
        if ("timestamp".equals(type)) {
            dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM,
                    locale(jspContext));
        } else if ("date".equals(type)) {
            dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, locale(jspContext));
        } else if ("hour".equals(type)) {
            dateFormat = DateFormat.getTimeInstance(DateFormat.MEDIUM, locale(jspContext));
        }/*from  w  w w  . j  a  v a  2 s .com*/
        List<String> patterns = new ArrayList<>();
        patterns.add("yyyy-MM-dd HH:mm:ss");
        patterns.add("yyyy-MM-dd");
        patterns.add("E MMM dd HH:mm:ss zzz yyyy");
        for (String pattern : patterns) {
            try {
                return dateFormat.format(new SimpleDateFormat(pattern, Locale.ENGLISH).parse(value));
            } catch (ParseException pe) {
                //Try another format
            }
        }
        return value;
    } else if ("currency".equals(type)) {
        DecimalFormat nf = new DecimalFormat("#,##0.00", new DecimalFormatSymbols(locale(jspContext)));
        return nf.format(new Double(value));
    } else if ("cep".equals(type)) {
        return String.format("%08d", Long.valueOf(value)).replaceAll("^([0-9]{5})([0-9]{3})$", "$1-$2");
    } else if ("cpf".equals(type)) {
        return String.format("%011d", Long.valueOf(value))
                .replaceAll("^([0-9]{3})([0-9]{3})([0-9]{3})([0-9]{2})$", "$1.$2.$3-$4");
    } else if ("cnpj".equals(type)) {
        return String.format("%014d", Long.valueOf(value))
                .replaceAll("^([0-9]{2})([0-9]{3})([0-9]{3})([0-9]{4})([0-9]{2})$", "$1.$2.$3/$4-$5");
    } else if ("tel".equals(type)) {
        return String.format("%010d", Long.valueOf(value)).replaceAll("^([0-9]{2})([0-9]{4,5})([0-9]{4})$",
                "($1) $2-$3");
    }
    return value;
}

From source file:it.reply.orchestrator.resource.common.AbstractResource.java

private String convertDate(Date date) {
    TimeZone tz = TimeZone.getTimeZone("UTC");
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mmZ");
    df.setTimeZone(tz);/*from   ww w. ja v a  2s .co  m*/
    return df.format(date);
}

From source file:au.com.redbarn.liferay.spring.mvc.portlet.HomeController.java

/**
 * Simply selects the home view to render by returning its name.
 *///from   w w w .jav  a2 s. c  om
@RenderMapping
public String home(Locale locale, Model model) {

    log.info("Welcome home! the client locale is " + locale.toString());

    Date date = new Date();
    DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);

    String formattedDate = dateFormat.format(date);

    model.addAttribute("serverTime", formattedDate);

    return "home";
}

From source file:com.satlabs.testbed.HomeController.java

/**
 * Simply selects the home view to render by returning its name.
 */// www  .  ja va 2 s .c om
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
    logger.info("Welcome home! The client locale is {}.", locale);

    Date date = new Date();
    DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);

    String formattedDate = dateFormat.format(date);

    model.addAttribute("serverTime", formattedDate);

    return "home";
}

From source file:es.tid.fiware.rss.service.CdrsManager.java

/**
 * Get File where save cdrs.// w  ww  .  j  av  a2  s  . com
 * 
 * @return
 * @throws IOException
 */
public File getFile() throws IOException {
    Date date = Calendar.getInstance().getTime();
    DateFormat formatter = new SimpleDateFormat("yyyyMMdd_hhmmss");
    String dateFormatted = formatter.format(date);
    String path = (String) rssProps.get("cdrfilepath");
    File cdrFile = new File(path + "fiwarecdr_" + dateFormatted + ".xml");
    cdrFile.createNewFile();
    logger.debug("File created: " + cdrFile.getCanonicalPath());
    return cdrFile;
}

From source file:com.wso2telco.dep.mediator.publisher.AbstractPublisher.java

protected String getCurrentDate() {
    Date currentDate = new Date();
    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    return dateFormat.format(currentDate);
}

From source file:com.skplanet.payment.sample.controller.SampleController.java

/**
 * Simply selects the home view to render by returning its name.
 */// w w  w. jav  a2 s.  com
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
    logger.info("Welcome home! The client locale is {}.", locale);

    Date date = new Date();
    DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);

    String formattedDate = dateFormat.format(date);

    model.addAttribute("serverTime", formattedDate);

    List<HashMap<String, String>> outputs = sqlSession.selectList("userControlMapper.selectSample");
    model.addAttribute("showDB", outputs.toString());

    return "home";
}

From source file:au.com.redbarn.liferay.portlet.property.HomeController.java

/**
 * Simply selects the home view to render by returning its name.
 */// w  ww.  j ava  2 s  .  c  o  m
@RenderMapping
public String home(RenderRequest request, Locale locale, Model model) {

    log.info("Welcome home! the client locale is " + locale.toString());

    model.addAttribute(Constants.MESSAGE_PARAM,
            request.getPreferences().getValue(Constants.MESSAGE_PARAM, Constants.DEFAULT_MESSAGE));

    Date date = new Date();
    DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);

    String formattedDate = dateFormat.format(date);

    model.addAttribute("serverTime", formattedDate);

    return "home";
}

From source file:org.openmrs.module.vcttrac.web.controller.VCTPreCounselingFormController.java

/**
 * @see org.springframework.web.servlet.mvc.ParameterizableViewController#handleRequestInternal(javax.servlet.http.HttpServletRequest,
 *      javax.servlet.http.HttpServletResponse)
 *///  w ww  . j av a2 s.c om
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
        throws Exception {
    ModelAndView mav = new ModelAndView(getViewName());
    VCTPreCounselingInfo ci = new VCTPreCounselingInfo();
    DateFormat sdf = Context.getDateFormat();
    Date today = new Date();

    try {
        ci.setEncounterDate(sdf.format(today));
        ci.setLocationId(Integer.valueOf(VCTConfigurationUtil.getDefaultLocationId()));
        //if (Context.getAuthenticatedUser().hasRole("Provider"))
        ci.setProviderId(Context.getAuthenticatedUser().getPerson().getPersonId());

        mav.addObject("ci", ci);
    } catch (Exception ex) {
        String msg = "An error occured [" + ex.getMessage() + "], please check your log file.";
        request.getSession().setAttribute(WebConstants.OPENMRS_ERROR_ATTR, msg);
        log.error(">>>>>>>>>>>>VCT>>Pre>>Counseling>>Form>>>> An errer occured : " + ex.getMessage());
        ex.printStackTrace();
    }
    return mav;
}

From source file:thymeleafexamples.extrathyme.web.controller.ExtraThymeController.java

@RequestMapping(value = "/ajaxshow")
public void ajaxShowTime(HttpServletRequest request, HttpServletResponse response) {
    Date date = new Date();
    DateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
    String timestring = "It is " + formatDate.format(date) + ".";

    try {/*from w w  w . j  a v a2  s .  c  o m*/
        response.setContentType("text/xml;charset=UTF-8");
        String str = "<?xml version=\"1.0\" ?> <clock><timestring>" + timestring + "</timestring></clock>";
        response.getWriter().write(str);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return;
}