Example usage for java.text SimpleDateFormat SimpleDateFormat

List of usage examples for java.text SimpleDateFormat SimpleDateFormat

Introduction

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

Prototype

public SimpleDateFormat(String pattern) 

Source Link

Document

Constructs a SimpleDateFormat using the given pattern and the default date format symbols for the default java.util.Locale.Category#FORMAT FORMAT locale.

Usage

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Box form = Box.createVerticalBox();
    form.add(new JLabel("Name:"));
    form.add(new JTextField("User Name"));

    form.add(new JLabel("Birthday:"));
    JFormattedTextField birthdayField = new JFormattedTextField(new SimpleDateFormat("MM/dd/yy"));
    birthdayField.setValue(new Date());
    form.add(birthdayField);//from   w  w  w .  ja  v a 2  s  . co m

    form.add(new JLabel("Age:"));
    form.add(new JFormattedTextField(new Integer(32)));

    form.add(new JLabel("Hairs on Body:"));
    JFormattedTextField hairsField = new JFormattedTextField(new DecimalFormat("###,###"));
    hairsField.setValue(new Integer(100000));
    form.add(hairsField);

    form.add(new JLabel("Phone Number:"));
    JFormattedTextField phoneField = new JFormattedTextField(new MaskFormatter("(###)###-####"));
    phoneField.setValue("(314)888-1234");
    form.add(phoneField);

    JFrame frame = new JFrame("User Information");
    frame.getContentPane().add(form);
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

From source file:FormattedSample.java

public static void main(final String args[]) {
    JFrame frame = new JFrame("Formatted Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    DateFormat displayFormat = new SimpleDateFormat("yyyy--MMMM--dd");
    DateFormatter displayFormatter = new DateFormatter(displayFormat);
    DateFormat editFormat = new SimpleDateFormat("MM/dd/yy");
    DateFormatter editFormatter = new DateFormatter(editFormat);
    DefaultFormatterFactory factory = new DefaultFormatterFactory(displayFormatter, displayFormatter,
            editFormatter);/*from w w w. java2  s .  c o m*/
    JFormattedTextField date2TextField = new JFormattedTextField(factory, new Date());
    frame.add(date2TextField, BorderLayout.NORTH);

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            JFormattedTextField source = (JFormattedTextField) actionEvent.getSource();
            Object value = source.getValue();
            System.out.println("Class: " + value.getClass());
            System.out.println("Value: " + value);
        }
    };
    date2TextField.addActionListener(actionListener);

    frame.add(new JTextField(), BorderLayout.SOUTH);
    frame.setSize(250, 100);
    frame.setVisible(true);
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    SimpleDateFormat xmlDateFormat = new SimpleDateFormat("MM.dd.yy");

    MapVariableResolver resolver = new MapVariableResolver();
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = dbf.newDocumentBuilder();
    Document document = builder.parse(new File("t.xml"));

    XPathFactory factory = XPathFactory.newInstance();
    XPath xPath = factory.newXPath();
    xPath.setXPathVariableResolver(resolver);
    XPathExpression expression = xPath.compile("/schedule/show[@date=$date]/guest");

    String formattedDate = xmlDateFormat.format(new Date(2006, 5, 14));
    resolver.addVariable(null, "date", formattedDate);
    Element guest = (Element) expression.evaluate(document, XPathConstants.NODE);

    System.out.println(guest.getElementsByTagName("name").item(0).getTextContent());
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    JFrame frame = new JFrame("Formatted Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    DateFormat displayFormat = new SimpleDateFormat("yyyy--MMMM--dd");
    DateFormatter displayFormatter = new DateFormatter(displayFormat);
    DateFormat editFormat = new SimpleDateFormat("MM/dd/yy");
    DateFormatter editFormatter = new DateFormatter(editFormat);
    DefaultFormatterFactory factory = new DefaultFormatterFactory(displayFormatter, displayFormatter,
            editFormatter);//from  www  .j av a2  s .c o  m
    JFormattedTextField date2TextField = new JFormattedTextField(factory, new Date());
    frame.add(date2TextField, BorderLayout.NORTH);

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            JFormattedTextField source = (JFormattedTextField) actionEvent.getSource();
            Object value = source.getValue();
            System.out.println("Class: " + value.getClass());
            System.out.println("Value: " + value);
        }
    };
    date2TextField.addActionListener(actionListener);
    frame.add(new JTextField(), BorderLayout.SOUTH);

    frame.setSize(250, 100);
    frame.setVisible(true);
}

From source file:uta.ak.ExecQuartzJob.java

public static void main(String[] args) throws Exception {

    SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("quartzContext.xml");
    Scheduler quartzScheduler = (Scheduler) applicationContext.getBean("quartzScheduler");

    JobDetail jobDetail = JobBuilder.newJob(CollectTwitterJob.class)
            .withIdentity("qrtz_job_collecttwitter", "qrtz_job_collecttwitter").build();
    SimpleScheduleBuilder builder = SimpleScheduleBuilder.simpleSchedule().repeatSecondlyForTotalCount(1000)
            .withIntervalInHours(24);//from w  ww.jav  a2s. c o  m

    Trigger trigger = TriggerBuilder.newTrigger()
            .withIdentity("qrtz_trigger_collecttwitter", "qrtz_trigger_collecttwitter")
            .startAt(format1.parse("2016-08-03 00:05:00")).withSchedule(builder).build();

    //        quartzScheduler.scheduleJob(jobDetail, trigger);  
}

From source file:Main.java

public static void main(String[] args) {
    List<String> yearList = new ArrayList<>(25);
    yearList.add("042015");
    yearList.add("052015");
    yearList.add("062015");
    yearList.add("072015");
    yearList.add("082015");
    yearList.add("092015");
    yearList.add("102010");
    yearList.add("112010");
    yearList.add("122010");
    yearList.add("012015");
    yearList.add("022015");
    yearList.add("032015");

    Collections.sort(yearList, new Comparator<String>() {
        private DateFormat format = new SimpleDateFormat("MMyyyy");

        @Override/*from  w ww  . j  a  v a 2  s  .c o m*/
        public int compare(String o1, String o2) {
            int result = 0;
            try {
                Date d1 = format.parse(o1);
                try {
                    Date d2 = format.parse(o2);
                    result = d1.compareTo(d2);
                } catch (ParseException ex) {
                    result = -1;
                }
            } catch (ParseException ex) {
                result = 1;
            }
            return result;
        }
    });
    System.out.println(yearList);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Calendar calendar = new GregorianCalendar();
    calendar.set(Calendar.HOUR_OF_DAY, 13); // 1pm

    SpinnerDateModel dateModel = new SpinnerDateModel(calendar.getTime(), null, null, Calendar.HOUR_OF_DAY);
    JSpinner spinner = new JSpinner(dateModel);

    JFormattedTextField tf = ((JSpinner.DefaultEditor) spinner.getEditor()).getTextField();
    DefaultFormatterFactory factory = (DefaultFormatterFactory) tf.getFormatterFactory();
    DateFormatter formatter = (DateFormatter) factory.getDefaultFormatter();

    // Change the date format to only show the hours
    formatter.setFormat(new SimpleDateFormat("hh:00 a"));
    //formatter.setFormat(new SimpleDateFormat("HH:00 a"));
}

From source file:ec.edu.espe.distribuidas.factnosql.test.consultaTotalVentas.java

public static void main(String[] args) throws ParseException {
    PersistenceManager persistence = new PersistenceManager();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Query q = persistence.context().createQuery(Factura.class).field("fechaEmision")
            .equal(sdf.parse("2016-01-27"));
    Iterator<VentasDiarias> aggregate = persistence.context().createAggregation(Factura.class)
            .group("fechaEmision", Group.grouping("total", Group.sum("total")),
                    Group.grouping("count", new Accumulator("$sum", 1)),
                    Group.grouping("fechaEmision", Group.first("fechaEmision")))
            .aggregate(VentasDiarias.class);
    List<VentasDiarias> totalVentas = IteratorUtils.toList(aggregate);
    for (VentasDiarias p : totalVentas)
        System.out.println(p);/*from w w  w  . ja  va 2 s  .co  m*/

}

From source file:ec.edu.espe.distribuidas.factnosql.test.consultaPersonas.java

public static void main(String[] args) throws ParseException {
    PersistenceManager persistence = new PersistenceManager();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Query q = persistence.context().createQuery(Factura.class).field("fechaEmision")
            .equal(sdf.parse("2016-01-27"));
    Iterator<ProductoSum> aggregate = persistence.context().createAggregation(Factura.class).match(q)
            .unwind("detalle")
            .group("detalle.producto", Group.grouping("count", Group.sum("detalle.cantidad")),
                    Group.grouping("fecha", Group.first("fechaEmision")),
                    Group.grouping("codigo", Group.first("detalle.codigo")))
            .aggregate(ProductoSum.class);
    List<ProductoSum> presonasS = IteratorUtils.toList(aggregate);
    for (ProductoSum p : presonasS)
        System.out.println(p);/*w w w. j a va  2s. c  o  m*/

}

From source file:Person.java

public static void main(String[] args) {
    SimpleDateFormat df = new SimpleDateFormat("mm-dd-yyyy");
    ArrayList<Person> people;
    people = new ArrayList<Person>();
    try {/*from w  w w .  j a v  a2 s  .co  m*/
        people.add(new Person("A", 9, df.parse("12-12-2014")));
        people.add(new Person("B", 2, df.parse("1-12-2013")));
        people.add(new Person("C", 4, df.parse("12-2-2012")));
    } catch (ParseException e) {
        e.printStackTrace();
    }

    Collections.sort(people, new CompId());
    System.out.println("BY ID");
    for (Person p : people) {
        System.out.println(p.toString());
    }

    Collections.sort(people, new CompDate(false));
    System.out.println("BY Date asc");
    for (Person p : people) {
        System.out.println(p.toString());
    }
    Collections.sort(people, new CompDate(true));
    System.out.println("BY Date desc");
    for (Person p : people) {
        System.out.println(p.toString());
    }

}