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.rodiontsev.maven.plugins.buildinfo.providers.BuildTimeProvider.java

@Override
public Map<String, String> getInfo(MavenProject project, BuildInfoMojo mojo) {
    Map<String, String> info = new LinkedHashMap<String, String>();

    String dateTimePattern = mojo.getDateTimePattern();
    if (StringUtils.isNotBlank(dateTimePattern)) {
        String buildTime;/*w  w w  .  j a  v a 2  s.  c  o  m*/
        try {
            DateFormat dateFormat = new SimpleDateFormat(dateTimePattern, Locale.ENGLISH);
            buildTime = dateFormat.format(new Date());
        } catch (IllegalArgumentException e) {
            buildTime = "the given pattern is invalid";
            mojo.getLog().warn(String.format(
                    "The given date-time pattern '%s' is invalid. Please read %s javadoc about user-defined patterns for date-time formatting.",
                    dateTimePattern, SimpleDateFormat.class.getCanonicalName()));
        }
        info.put("build.time", buildTime);
    }

    return info;
}

From source file:org.openinfinity.web.controller.HomeController.java

/**
 * Simply selects the home view to render by returning its name.
 *//*from w  ww.jav a  2s .co m*/
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
    LOGGER.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.jwt.controllers.utils.CurrentDateController.java

/**
 * @return// w w w  . ja va2s .  c  o  m
 */
@RequestMapping(method = RequestMethod.GET, produces = "application/json")
@ResponseBody()
public Map<String, String> getCurrentDate() {
    Map<String, String> date = new HashMap<String, String>();

    DateFormat df = new SimpleDateFormat("dd/MM/YYYY");

    Date today = Calendar.getInstance().getTime();
    String reportDate = df.format(today);
    date.put("currentDate", reportDate);
    return date;
}

From source file:com.haulmont.cuba.gui.components.formatters.DateFormatter.java

@Override
public String format(Date value) {
    if (value == null) {
        return null;
    }//from  www  .j  ava  2s.  c o m
    String format = element.attributeValue("format");
    if (StringUtils.isBlank(format)) {
        String type = element.attributeValue("type");
        if (type != null) {
            FormatStrings formatStrings = Datatypes.getFormatStrings(userSessionSource.getLocale());
            if (formatStrings == null)
                throw new IllegalStateException(
                        "FormatStrings are not defined for " + userSessionSource.getLocale());
            switch (type) {
            case "DATE":
                format = formatStrings.getDateFormat();
                break;
            case "DATETIME":
                format = formatStrings.getDateTimeFormat();
                break;
            default:
                throw new RuntimeException("Illegal formatter type value");
            }
        }
    }

    if (StringUtils.isBlank(format)) {
        return value.toString();
    } else {
        if (format.startsWith("msg://")) {
            format = messages.getMainMessage(format.substring(6, format.length()));
        }
        DateFormat df = new SimpleDateFormat(format);
        return df.format(value);
    }
}

From source file:com.intuit.tank.harness.functions.JexlDateFunctions.java

/**
 * Get the current date./*from   w  ww .ja  va2  s. c  o  m*/
 * 
 * @param format
 *            The format of the response (MM-dd-yyyy, MMddyyyy, etc)
 * @return The current date
 */
public String currentDate(String format) {
    DateFormat formatter = getFormatter(format);
    return formatter.format(new Date());
}

From source file:com.gelecekonline.web.HomeController.java

/**
 * Simply selects the home view to render by returning its name.
 *///from  w w w.  j  av a  2s  .co m
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
    logger.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:org.openmrs.module.metadatasharing.web.taglib.handler.ObjectHandlerTag.java

/**
 * @see javax.servlet.jsp.tagext.SimpleTag#doTag()
 *//*  ww w.  j a v a  2s.  c  o m*/
@Override
public void doTag() throws JspException, IOException {
    String output = "";

    if (object != null) {
        if (get.equals("uuid")) {
            output = Handler.getUuid(object);
        } else if (get.equals("type")) {
            output = Handler.getRegisteredType(object);
        } else if (get.equals("name")) {
            output = Handler.getName(object);
        } else if (get.equals("description")) {
            output = Handler.getDescription(object);
        } else if (get.equals("dateChanged")) {
            if (Handler.getDateChanged(object) != null) {
                DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                output = dateFormat.format(Handler.getDateChanged(object));
            } else {
                output = "";
            }
        }
    }
    output = HtmlUtils.htmlEscape(output);
    getJspContext().getOut().print(output);
}

From source file:com.intuit.tank.harness.functions.JexlDateFunctions.java

/**
 * Get the date x days from now.//from w ww  .j  a  va 2 s  .  c o m
 * 
 * @param days
 *            The number of days to add (negative number to remove)
 * @param format
 *            The format of the response (MM-dd-yyyy, MMddyyyy, etc)
 * @return The new date
 */
public String addDays(Object odays, String format) {
    int days = FunctionHandler.getInt(odays);
    Calendar now = Calendar.getInstance();
    now.add(Calendar.DAY_OF_YEAR, days);
    DateFormat formatter = getFormatter(format);
    return formatter.format(now.getTime());
}

From source file:com.joshlong.esb.springintegration.modules.social.twitter.test.TwitterProducer.java

public String tweet() {
    Date d = new Date();
    DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);

    return String.format("The time is %s and all is well!", dateFormat.format(d));
}

From source file:com.google.code.gerrytan.jsonsimple.HomeController.java

/**
 * Simply selects the home view to render by returning its name.
 */// w w  w  .  j  a  v  a  2s .c  o m
@RequestMapping(value = "/home", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
    logger.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";
}