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 Exception {
    String today = "21/12/2007";

    DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
    Date date = formatter.parse(today);
    long dateInLong = date.getTime();

    System.out.println("date = " + date);
    System.out.println("dateInLong = " + dateInLong);
}

From source file:Main.java

public static void main(String[] args) {
    try {/*  w w  w  .j a  v a  2s.c  om*/

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date1 = sdf.parse("2015-12-31");
        Date date2 = sdf.parse("2015-01-31");

        System.out.println(sdf.format(date1));
        System.out.println(sdf.format(date2));

        if (date1.after(date2)) {
            System.out.println("Date1 is after Date2");
        }

        if (date1.before(date2)) {
            System.out.println("Date1 is before Date2");
        }

        if (date1.equals(date2)) {
            System.out.println("Date1 is equal Date2");
        }

    } catch (ParseException ex) {
        ex.printStackTrace();
    }
}

From source file:MainClass.java

public static void main(String[] args) {
    String pattern = "MM/dd/yyyy";
    SimpleDateFormat format = new SimpleDateFormat(pattern);
    try {//from w ww .j  a v a 2  s  .  c o m
        Date date = format.parse("12/31/2006");
        System.out.println(date);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    // formatting
    System.out.println(format.format(new Date()));
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String dStr = "Wed, 05 Jun 2015 00:48:12 GMT";
    SimpleDateFormat ft = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z");
    Date t = ft.parse(dStr);//from  ww  w  .java2  s .  co  m
    TimeZone gmt = TimeZone.getTimeZone("England/London");
    ft.setTimeZone(gmt);
    System.out.println(t);
    System.out.println(ft.format(t));
}

From source file:Main.java

public static void main(String[] args) {

    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:00Z");
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.YEAR, 2015);
    cal.set(Calendar.MONTH, Calendar.SEPTEMBER);
    cal.set(Calendar.DATE, 1);//from  w  w w. ja v  a  2  s.c  o m
    cal.set(Calendar.HOUR_OF_DAY, 12);
    cal.set(Calendar.MINUTE, 15);
    Date date = cal.getTime();
    System.out.println(date);
    TimeZone tz = TimeZone.getTimeZone("IST");
    dateFormat.setTimeZone(tz);
    String actual = dateFormat.format(date);

    System.out.println(actual);
}

From source file:CompareDates.java

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

    DateFormat df = new SimpleDateFormat("yyyy-MM-dd");

    // Get Date 1
    Date d1 = df.parse("2000-02-01");

    // Get Date 2
    Date d2 = df.parse("2001-03-02");

    String relation;//from   w  ww . j av a  2s.  c o  m
    if (d1.equals(d2))
        relation = "the same date as";
    else if (d1.before(d2))
        relation = "before";
    else
        relation = "after";
    System.out.println(d1 + " is " + relation + ' ' + d2);
}

From source file:Main.java

public static void main(String[] argv) {
    JFormattedTextField f = new JFormattedTextField(new SimpleDateFormat("yyyy-M-d"));
    f.setValue(new Date());

    DateFormatter fmt = (DateFormatter) f.getFormatter();
    fmt.setFormat(new SimpleDateFormat("d/M/yyyy"));

    f.setValue(f.getValue());//from  w w w.  j a v  a2  s .  co  m
}

From source file:Main.java

public static void main(String[] args) throws ParseException {
    String oldstring = "2013-11-15 00:00:00.0";

    java.util.Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S").parse(oldstring);

    String newstring = new SimpleDateFormat("yyyy-MM-dd").format(date);

    System.out.println(newstring);

}

From source file:Main.java

public static void main(String[] args) {
    Date d = new Date();
    System.out.println(d);/*www .  ja v  a  2s . co  m*/

    DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    df.setTimeZone(TimeZone.getTimeZone("Australia/Sydney"));
    System.out.println(df.format(d));
    df.setTimeZone(TimeZone.getTimeZone("Europe/London"));
    System.out.println(df.format(d));
}

From source file:Main.java

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

    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");

    String oeStartDateStr = "04/01/2015";
    String oeEndDateStr = "11/14/2015";

    Calendar cal = Calendar.getInstance();
    Integer year = cal.get(Calendar.YEAR);

    oeStartDateStr = oeStartDateStr.concat(year.toString());
    oeEndDateStr = oeEndDateStr.concat(year.toString());

    Date startDate = sdf.parse(oeStartDateStr);
    Date endDate = sdf.parse(oeEndDateStr);
    Date d = new Date();
    String currDt = sdf.format(d);

    if ((d.after(startDate) && (d.before(endDate)))
            || (currDt.equals(sdf.format(startDate)) || currDt.equals(sdf.format(endDate)))) {
        System.out.println("Date is between 1st april to 14th nov...");
    } else {/*  w w  w .  j a va2  s.com*/
        System.out.println("Date is not between 1st april to 14th nov...");
    }
}