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[] a) {
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    String input[] = { "2013-10-01 Vancouver, B.C.", "1248-03-01 Ottawa, ON", "1323-06-06 Toronto, ON" };
    for (int i = 0; i < input.length; i++) {
        ParsePosition pp = new ParsePosition(0);
        Date d = formatter.parse(input[i], pp);
        if (d == null) {
            System.err.println("Invalid date in " + input[i]);
            continue;
        }//  w  w w.  j a va 2  s .  co  m
        String location = input[i].substring(pp.getIndex());
        System.out.println(" on " + d + " in " + location);

    }
}

From source file:MainClass.java

public static void main(String[] args) throws ParseException {
    int year = 2003;
    int month = 12;
    int day = 12;

    String date = year + "/" + month + "/" + day;
    java.util.Date utilDate = null;

    try {/*w w  w  .  j av a  2 s .  c o  m*/
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
        utilDate = formatter.parse(date);
        System.out.println("utilDate:" + utilDate);
    } catch (ParseException e) {
        System.out.println(e.toString());
        e.printStackTrace();
    }

}

From source file:Main.java

public static void main(String[] args) {
    GregorianCalendar gc = new GregorianCalendar(2010, Calendar.SEPTEMBER, 9);
    Date birthDate = gc.getTime();

    String pattern = "'I was born on the day' dd 'of the month 'MMMM 'in' yyyy";

    SimpleDateFormat simpleFormatter = new SimpleDateFormat(pattern);
    System.out.println(simpleFormatter.format(birthDate));
}

From source file:DateFormatApp.java

public static void main(String args[]) {
    String pattern = "'The year is '";
    pattern += "yyyy GG";
    pattern += "'.\nThe month is '";
    pattern += "MMMMMMMMM";
    pattern += "'.\nIt is '";
    pattern += "hh";
    pattern += "' o''clock '";
    pattern += "a, zzzz";
    pattern += "'.'";
    SimpleDateFormat format = new SimpleDateFormat(pattern);
    String formattedDate = format.format(new Date());
    System.out.println(formattedDate);
}

From source file:Main.java

public static void main(String[] args) {
    long time = System.currentTimeMillis();
    Date d = new Date(time);
    Timestamp t = new Timestamp(time);
    t.setNanos(123456789);/*from   w  ww. j a va2  s .  c  o  m*/
    System.out.println(d);
    System.out.println(t);
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss'.'");
    NumberFormat nf = new DecimalFormat("000000000");
    System.out.println(df.format(t.getTime()) + nf.format(t.getNanos()));
}

From source file:Main.java

public static void main(String[] args) throws java.lang.Exception {
    String[] StartTimes = { "10:00", "7:00" };
    String[] EndTimes = { "12:00", "14:56" };
    for (int i = 0; i < StartTimes.length; i++) {
        if (StartTimes != null && StartTimes.length > 0 && EndTimes != null && EndTimes.length > 0) {
            SimpleDateFormat format = new SimpleDateFormat("HH:mm");
            Date date1 = format.parse(StartTimes[i]);
            Date date2 = format.parse(EndTimes[i]);
            long millis = date2.getTime() - date1.getTime();

            String hourminute = String.format("%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis),
                    TimeUnit.MILLISECONDS.toMinutes(millis)
                            - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)));
            System.out.println(hourminute);
        }//from  www.  j av a2 s  .  com
    }
}

From source file:FormattedSample.java

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

    JPanel datePanel = new JPanel(new BorderLayout());
    DateFormat format = new SimpleDateFormat("yyyy--MMMM--dd");
    JFormattedTextField dateTextField = new JFormattedTextField(format);
    datePanel.add(dateTextField, BorderLayout.CENTER);
    frame.add(datePanel, BorderLayout.NORTH);

    frame.add(new JTextField(), BorderLayout.SOUTH);
    frame.setSize(250, 100);//ww w .  j  av a  2  s.c  om
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {

    Date today = new Date();

    Calendar calendar = Calendar.getInstance();
    calendar.setTime(today);/*from  www . j  a va  2s  .c om*/

    calendar.add(Calendar.MONTH, 1);
    calendar.set(Calendar.DAY_OF_MONTH, 1);
    calendar.add(Calendar.DATE, -1);

    Date lastDayOfMonth = calendar.getTime();

    DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    System.out.println("Today            : " + sdf.format(today));
    System.out.println("Last Day of Month: " + sdf.format(lastDayOfMonth));
}

From source file:Main.java

public static void main(String[] args) {
    DateFormatter[] formatters = new DateFormatter[] { new DateFormatter(new SimpleDateFormat("dd.MM.yyyy")),
            new DateFormatter(new SimpleDateFormat("ddMMyyyy")),
            new DateFormatter(new SimpleDateFormat("yyyy.MM.dd.")),
            new DateFormatter(new SimpleDateFormat("dd.MMM.yyyy")),
            new DateFormatter(new SimpleDateFormat("dd/MM/yyyy")) };
    MultiFormatter<DateFormatter> multiFormatter = new MultiFormatter<>(formatters);

    JFormattedTextField ftf = new JFormattedTextField(multiFormatter);
    ftf.setColumns(10);//from www  . j  a  va2  s .co m

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(ftf);
    frame.add(new JButton("OK"));
    frame.pack();
    frame.setVisible(true);
}

From source file:DefaultFormatterFactoryDemo.java

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

    JLabel label = new JLabel("Date");

    DateFormat displayFormat = new SimpleDateFormat("yyyy--MMMM--dd");
    DateFormatter displayFormatter = new DateFormatter(displayFormat);
    DateFormat editFormat = new SimpleDateFormat("MM/dd/yy");
    DateFormatter editFormatter = new DateFormatter(editFormat);
    DateFormat nullFormat = new SimpleDateFormat("'null'");
    DateFormatter nullFormatter = new DateFormatter(nullFormat);
    DefaultFormatterFactory factory = new DefaultFormatterFactory(displayFormatter, displayFormatter,
            editFormatter, nullFormatter);

    JFormattedTextField input = new JFormattedTextField(factory);
    input.setColumns(30);//from w w  w  .  ja va 2 s.c  o  m
    JPanel panel = new JPanel();
    panel.add(label);
    panel.add(input);
    frame.add(panel, "North");

    frame.add(new JTextField(), "Center");
    frame.pack();
    frame.setVisible(true);
}