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:Main.java

public static void main(String[] args) throws java.text.ParseException {
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

    out.println(isBeforeMonths(-1, sdf.parse("14/12/2015")));
    out.println(isBeforeMonths(1, new Date()));
    out.println(isBeforeMonths(-1, sdf.parse("24/12/2015")));
}

From source file:Main.java

public static void main(String[] args) throws ParseException {
    SimpleDateFormat dateFormat = new SimpleDateFormat("ddMMyyyy");
    Calendar cal = Calendar.getInstance();
    String today = dateFormat.format(cal.getTime());
    System.out.println(today);//from w w  w.j a v  a 2  s. c o  m

    SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyy");
    Date actualdate = sdf.parse(today);
    Date date1 = sdf.parse("20042014");
    Date date2 = sdf.parse("23042015");
    Calendar actual = Calendar.getInstance();
    Calendar cal1 = Calendar.getInstance();
    Calendar cal2 = Calendar.getInstance();
    actual.setTime(actualdate);
    cal1.setTime(date1);
    cal2.setTime(date2);

    System.out.println(actual);
    System.out.println(cal1);
    System.out.println(cal2);

    if (actualdate.after(date1) && actualdate.before(date2)) {
        System.out.println("Yes");
    } else {
        System.out.println("No");
    }

    if (actual.after(cal1) && actual.before(cal2)) {
        System.out.println("Yes");
    } else {
        System.out.println("No");
    }

}

From source file:Main.java

public static void main(String args[]) {
    Date todaysDate = new java.util.Date();
    // Formatting date into yyyy-MM-dd HH:mm:ss e.g 2015-10-10 11:21:10

    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String formattedDate = formatter.format(todaysDate);
    System.out.println("Formatted date is ==>" + formattedDate);

    // Formatting date into yyyy-MM-dd e.g 2015-10-10
    formatter = new SimpleDateFormat("yyyy-MM-dd");
    formattedDate = formatter.format(todaysDate);
    System.out.println("Formatted date is ==>" + formattedDate);

    // Formatting date into MM/dd/yyyy e.g 10/10/2015
    formatter = new SimpleDateFormat("MM/dd/yyyy");
    formattedDate = formatter.format(todaysDate);
    System.out.println("Formatted date is ==>" + formattedDate);
}

From source file:Main.java

public static void main(String args[]) {
    int days = 1;
    int month = 1;
    int year = 2001;
    SimpleDateFormat sdf = new SimpleDateFormat("E dd-MM-yyyy G");
    StringBuffer buf = new StringBuffer();
    Calendar cal = new GregorianCalendar();
    cal.set(year, month - 1, days);//ww  w.ja v a  2  s. c  o  m
    sdf.format(cal.getTime(), buf, new FieldPosition(10));
    System.out.println(buf.toString());
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Date birth = null;/*from w  ww .  ja v  a2 s. c  o  m*/
    String birthDate = "30-MAR-2012";
    DateFormat formatter = null;
    formatter = new SimpleDateFormat("dd-MMM-yyyy");
    birth = (Date) formatter.parse(birthDate); // birtDate is a string
    if (birth == null) {
        System.out.println("Birth object is still null.");
    } else {
        System.out.println("Default date format " + birth);
        System.out.println("Our SimpleDateFormat " + formatter.format(birth));
        System.out.println("Our SimpleDateFormat with all uppercase " + formatter.format(birth).toUpperCase());
    }
}

From source file:Main.java

public static void main(final String[] args) {
    final String date = "2015-10-02T12:23:23";
    final String pattern = "yyyy-MM-dd'T'hh:mm:ss";
    final SimpleDateFormat sdf = new SimpleDateFormat(pattern);
    try {//from w w  w  .j a va  2s  .  com
        Date d = sdf.parse(date);
        System.out.println(d);
    } catch (ParseException e) {
        e.printStackTrace();
    }

}

From source file:Main.java

public static void main(String[] args) {
    Date date = Calendar.getInstance().getTime();

    // Display a date in day, month, year format
    DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
    String today = formatter.format(date);
    System.out.println("Today : " + today);
}

From source file:Main.java

License:asdf

public static void main(String[] args) {
    String[] tests = { "01/23/1983", "1/23/1983", "asdf/3/2" };

    String formatString = "MM/dd/yyyy";

    SimpleDateFormat sdf = new SimpleDateFormat(formatString);

    for (String test : tests) {
        Date date = null;/*from  w  ww  . j a  v  a 2 s  .  c o  m*/
        try {
            date = sdf.parse(test);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.println(date);
    }
}

From source file:Main.java

public static void main(String[] args) {
    Calendar cal = Calendar.getInstance();
    System.out.println("Calendar:" + cal.toString());
    Date d = cal.getTime();/*from w w  w.j a  v a2 s . c o m*/
    SimpleDateFormat sdf = new SimpleDateFormat("DD/MM/yyyy HH:mm:ss");
    System.out.println(sdf.format(d));
    SimpleDateFormat sdfNew = new SimpleDateFormat("HH:mm:ss");
    System.out.println(sdfNew.format(d));
}

From source file:Main.java

public static void main(String[] args) {
    String text = "ab01/01/1999cd12/31/2000ef";
    String pattern = "MM/dd/yyyy";

    SimpleDateFormat simpleFormatter = new SimpleDateFormat(pattern);

    // Set  the   start index   at 2
    ParsePosition startPos = new ParsePosition(2);

    // Parse the   text to get   the   first date (January 1,  1999) 
    Date firstDate = simpleFormatter.parse(text, startPos);
    System.out.println(firstDate);

    //Now, startPos has  its index set after the last  character of the first date parsed.

    int currentIndex = startPos.getIndex();
    System.out.println(currentIndex);
    // To set its index   to the   next   date increment its index   by  2. 

    int nextIndex = currentIndex + 2;
    startPos.setIndex(nextIndex);//from ww w  .  j  a  va  2  s  .  c om

    // Parse the   text to get   the   second  date (December  31,   2000) 
    Date secondDate = simpleFormatter.parse(text, startPos);
    System.out.println(secondDate);

}