Example usage for java.text DateFormat getTimeInstance

List of usage examples for java.text DateFormat getTimeInstance

Introduction

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

Prototype

public static final DateFormat getTimeInstance(int style, Locale aLocale) 

Source Link

Document

Gets the time formatter with the given formatting style for the given locale.

Usage

From source file:EventObject.java

public static void main(String[] args) {
    JFrame f = new JFrame();
    JButton ok = new JButton("Ok");

    ok.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            Calendar cal = Calendar.getInstance();
            cal.setTimeInMillis(event.getWhen());
            Locale locale = Locale.getDefault();
            String s = DateFormat.getTimeInstance(DateFormat.SHORT, locale).format(new Date());

            if (event.getID() == ActionEvent.ACTION_PERFORMED)
                System.out.println(" Event Id: ACTION_PERFORMED");

            System.out.println(" Time: " + s);

            String source = event.getSource().getClass().getName();
            System.out.println(" Source: " + source);

            int mod = event.getModifiers();
            if ((mod & ActionEvent.ALT_MASK) > 0)
                System.out.println("Alt ");

            if ((mod & ActionEvent.SHIFT_MASK) > 0)
                System.out.println("Shift ");

            if ((mod & ActionEvent.META_MASK) > 0)
                System.out.println("Meta ");

            if ((mod & ActionEvent.CTRL_MASK) > 0)
                System.out.println("Ctrl ");

        }/*ww  w  .  j  av  a2  s .  c  o m*/
    });

    f.add(ok);

    f.setSize(420, 250);
    f.setLocationRelativeTo(null);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
}

From source file:com.hp.mqm.atrf.Main.java

public static void main(String[] args) {

    long start = System.currentTimeMillis();
    setUncaughtExceptionHandler();/*  w  w  w.j  a va2 s  .c om*/

    CliParser cliParser = new CliParser();
    cliParser.handleHelpAndVersionOptions(args);

    configureLog4J();
    logger.info(System.lineSeparator() + System.lineSeparator());
    logger.info("************************************************************************************");
    DateFormat dateFormatter = DateFormat.getDateInstance(DateFormat.DEFAULT, Locale.getDefault());
    DateFormat timeFormatter = DateFormat.getTimeInstance(DateFormat.DEFAULT, Locale.getDefault());
    logger.info((String.format("Starting HPE ALM Test Result Collection Tool %s %s",
            dateFormatter.format(new Date()), timeFormatter.format(new Date()))));
    logger.info("************************************************************************************");

    FetchConfiguration configuration = cliParser.parse(args);
    ConfigurationUtilities.setConfiguration(configuration);

    App app = new App(configuration);
    app.start();

    long end = System.currentTimeMillis();
    logger.info(String.format("Finished creating tests and test results on ALM Octane in %s seconds",
            (end - start) / 1000));
    logger.info(System.lineSeparator());
}

From source file:com.vityuk.ginger.provider.format.JdkDateUtils.java

private static DateFormat createJdkDateFormat(FormatType formatType, DateFormatStyle formatStyle,
        Locale locale) {/*from  w w  w  .ja  v a 2 s .c om*/
    int style = createJdkDateStyleCode(formatStyle);
    switch (formatType) {
    case TIME:
        return DateFormat.getTimeInstance(style, locale);
    case DATE:
        return DateFormat.getDateInstance(style, locale);
    case DATETIME:
        return DateFormat.getDateTimeInstance(style, style, locale);
    }
    throw new IllegalArgumentException();
}

From source file:Main.java

public void actionPerformed(ActionEvent e) {

    Locale locale = Locale.getDefault();
    Date date = new Date(e.getWhen());
    String s = DateFormat.getTimeInstance(DateFormat.SHORT, locale).format(date);

    if (!model.isEmpty()) {
        model.clear();/*w  w w . j  av a2s  .  c o m*/
    }

    if (e.getID() == ActionEvent.ACTION_PERFORMED) {
        model.addElement(" Event Id: ACTION_PERFORMED");

    }

    model.addElement("Time: " + s);

    String source = e.getSource().getClass().getName();

    int mod = e.getModifiers();

    StringBuffer buffer = new StringBuffer("Modifiers: ");

    if ((mod & ActionEvent.ALT_MASK) > 0) {
        buffer.append("Alt ");

    }

    if ((mod & ActionEvent.SHIFT_MASK) > 0) {
        buffer.append("Shift ");

    }

    if ((mod & ActionEvent.META_MASK) > 0) {
        buffer.append("Meta ");

    }

    if ((mod & ActionEvent.CTRL_MASK) > 0) {
        buffer.append("Ctrl ");

    }
    model.addElement(buffer);

}

From source file:org.openmrs.util.Format.java

public static String format(Date date, Locale locale, FORMAT_TYPE type) {
    log.debug("Formatting date: " + date + " with locale " + locale);

    DateFormat dateFormat = null;

    if (type == FORMAT_TYPE.TIMESTAMP) {
        dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
    } else if (type == FORMAT_TYPE.TIME) {
        dateFormat = DateFormat.getTimeInstance(DateFormat.MEDIUM, locale);
    } else {/*from  w w w.j  a v  a 2  s .c o  m*/
        dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, locale);
    }
    return date == null ? "" : dateFormat.format(date);
}

From source file:org.jamwiki.utils.DateUtil.java

/**
 * Format the given date using the given pattern to return the date
 * as a formatted string.  If the pattern is invalid this method will
 * return <code>null</code>.
 *//*from   w  ww  .j av  a2 s .c o m*/
public static String formatDate(Date date, String pattern, String localeString, String timeZoneString,
        DateFormatType dateFormatType) {
    Locale locale = DateUtil.stringToLocale(localeString);
    TimeZone tz = DateUtil.stringToTimeZone(timeZoneString);
    SimpleDateFormat sdf = null;
    int style = DateUtil.stringToDateFormatStyle(pattern);
    if (style != -1 && dateFormatType == DateFormatType.DATE_ONLY) {
        sdf = (SimpleDateFormat) DateFormat.getDateInstance(style, locale);
    } else if (style != -1 && dateFormatType == DateFormatType.TIME_ONLY) {
        sdf = (SimpleDateFormat) DateFormat.getTimeInstance(style, locale);
    } else if (style != -1 && dateFormatType == DateFormatType.DATE_AND_TIME) {
        sdf = (SimpleDateFormat) DateFormat.getDateTimeInstance(style, style, locale);
    } else {
        try {
            sdf = new SimpleDateFormat(pattern, locale);
        } catch (IllegalArgumentException e) {
            String msg = "Attempt to format date with invalid pattern " + pattern
                    + ". If you have customized date or time formats in your "
                    + "jamwiki-configuration.xml file please verify that they are "
                    + "valid java.text.SimpleDateFormat patterns.";
            logger.warn(msg, e);
            return null;
        }
    }
    sdf.setTimeZone(tz);
    return sdf.format(date);
}

From source file:de.interseroh.report.formatters.TimeFormatter.java

protected DateFormat getFormatterInstance(Locale locale) {
    return DateFormat.getTimeInstance(DateFormat.SHORT, locale);
}

From source file:DigitalClock.java

public DigitalClock() {
    // Set default values for our properties
    setFormat(DateFormat.getTimeInstance(DateFormat.MEDIUM, getLocale()));
    setUpdateFrequency(1000); // Update once a second

    // Specify a Swing TransferHandler object to do the dirty work of
    // copy-and-paste and drag-and-drop for us. This one will transfer
    // the value of the "time" property. Since this property is read-only
    // it will allow drags but not drops.
    setTransferHandler(new TransferHandler("time"));

    // Since JLabel does not normally support drag-and-drop, we need an
    // event handler to detect a drag and start the transfer.
    addMouseMotionListener(new MouseMotionAdapter() {
        public void mouseDragged(MouseEvent e) {
            getTransferHandler().exportAsDrag(DigitalClock.this, e, TransferHandler.COPY);
        }//  ww w .  j  a  v  a 2 s.  c  o  m
    });

    // Before we can have a keyboard binding for a Copy command,
    // the component needs to be able to accept keyboard focus.
    setFocusable(true);
    // Request focus when we're clicked on
    addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent e) {
            requestFocus();
        }
    });
    // Use a LineBorder to indicate when we've got the keyboard focus
    addFocusListener(new FocusListener() {
        public void focusGained(FocusEvent e) {
            setBorder(LineBorder.createBlackLineBorder());
        }

        public void focusLost(FocusEvent e) {
            setBorder(null);
        }
    });

    // Now bind the Ctrl-C keystroke to a "Copy" command.
    InputMap im = new InputMap();
    im.setParent(getInputMap(WHEN_FOCUSED));
    im.put(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK), "Copy");
    setInputMap(WHEN_FOCUSED, im);

    // And bind the "Copy" command to a pre-defined Action that performs
    // a copy using the TransferHandler we've installed.
    ActionMap am = new ActionMap();
    am.setParent(getActionMap());
    am.put("Copy", TransferHandler.getCopyAction());
    setActionMap(am);

    // Create a javax.swing.Timer object that will generate ActionEvents
    // to tell us when to update the displayed time. Every updateFrequency
    // milliseconds, this timer will cause the actionPerformed() method
    // to be invoked. (For non-GUI applications, see java.util.Timer.)
    timer = new Timer(updateFrequency, new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            setText(getTime()); // set label to current time string
        }
    });
    timer.setInitialDelay(0); // Do the first update immediately
    timer.start(); // Start timing now!
}

From source file:com.appeaser.sublimepicker.SublimePickerFragment.java

public SublimePickerFragment() {
    // Initialize formatters
    mDateFormatter = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.getDefault());
    mTimeFormatter = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.getDefault());
    mTimeFormatter.setTimeZone(TimeZone.getTimeZone("GMT+0"));
}

From source file:org.idansof.otp.client.Planner.java

public PlanResult generatePlan(PlanRequest planRequest)
        throws IOException, XmlPullParserException, ParseException {

    AndroidHttpClient androidHttpClient = AndroidHttpClient.newInstance("");
    try {/* w w w .  j av  a 2s  .  c om*/

        DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, locale);
        DateFormat timeFormat = DateFormat.getTimeInstance(DateFormat.SHORT, locale);

        Uri.Builder builder = Uri.parse("http://" + host).buildUpon();

        builder.appendEncodedPath(uri);
        builder.appendQueryParameter("fromPlace", planRequest.getFrom().getCoordinateString());
        builder.appendQueryParameter("toPlace", planRequest.getTo().getCoordinateString());
        builder.appendQueryParameter("date", dateFormat.format(planRequest.getDate()));
        builder.appendQueryParameter("time", timeFormat.format(planRequest.getDate()));

        HttpProtocolParams.setContentCharset(androidHttpClient.getParams(), "utf-8");
        String uri = builder.build().toString();
        Log.i(Planner.class.toString(), "Fetching plan from " + uri);
        HttpUriRequest httpUriRequest = new HttpGet(uri);
        httpUriRequest.setHeader("Accept", "text/xml");
        HttpResponse httpResponse = androidHttpClient.execute(httpUriRequest);

        InputStream contentStream = httpResponse.getEntity().getContent();
        Log.i(Planner.class.toString(),
                "Parsing content , size :" + httpResponse.getEntity().getContentLength());
        return parseXMLResponse(contentStream);
    } finally {
        androidHttpClient.close();
    }
}