Example usage for java.text SimpleDateFormat format

List of usage examples for java.text SimpleDateFormat format

Introduction

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

Prototype

public final String format(Date date) 

Source Link

Document

Formats a Date into a date-time string.

Usage

From source file:Main.java

public static void main(String args[]) {
    SimpleDateFormat df = new SimpleDateFormat("dd/MM/yy");
    java.util.Date date = new Date();
    df.applyPattern("EEE");
    String day = df.format(date);
    if (day.compareTo("Sat") == 0 || day.compareTo("Sun") == 0) {
        System.out.println(day + ": Weekend");
    } else {/* w  w w  . j  a v  a  2  s  .com*/
        System.out.println(day + ": Weekday");
    }
}

From source file:DateDemo.java

public static void main(String[] args) {
    //+// w ww .  j a  v a2 s . com
    Date dNow = new Date();

    /* Simple, Java 1.0 date printing */
    System.out.println("It is now " + dNow.toString());

    // Use a SimpleDateFormat to print the date our way.
    SimpleDateFormat formatter = new SimpleDateFormat("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
    System.out.println("It is " + formatter.format(dNow));
    //-
}

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   ww  w .j  av  a2s.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) throws Exception {
    String inputText = "Nov 10,2015";

    TimeZone utc = TimeZone.getTimeZone("UTC");

    SimpleDateFormat inputFormat = new SimpleDateFormat("MMM d,yyyy", Locale.US);
    inputFormat.setTimeZone(utc);/*from   w w w  .  jav  a2s. c  om*/

    SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
    outputFormat.setTimeZone(utc);

    Date parsed = inputFormat.parse(inputText);
    String outputText = outputFormat.format(parsed);

    System.out.println(outputText);
}

From source file:Main.java

public static void main(String[] args) {
    try {//from w  w w .j  ava  2 s  .co  m
        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.compareTo(date2) > 0) {
            System.out.println("Date1 is after Date2");
        } else if (date1.compareTo(date2) < 0) {
            System.out.println("Date1 is before Date2");
        } else if (date1.compareTo(date2) == 0) {
            System.out.println("Date1 is equal to Date2");
        } else {
            System.out.println("How to get here?");
        }

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

From source file:Main.java

public static void main(String[] args) {
    try {//  ww  w  .ja  va2 s  .  co  m

        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:fitmon.Client.java

public static void main(String[] args) throws IOException, ClientProtocolException, NoSuchAlgorithmException,
        InvalidKeyException, SAXException, ParserConfigurationException {
    Food food = null;/*from  w  w w  .j  a  v  a  2  s .co  m*/
    DietAPI dApi = new DietAPI();
    JDBCConnection jdbcCon = new JDBCConnection();
    ArrayList<Food> foodItems = dApi.getFood();
    Scanner scan = new Scanner(System.in);
    Calendar currentDate = Calendar.getInstance(); //Get the current date
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd"); //format it as per your requirement
    String dateNow = formatter.format(currentDate.getTime());
    System.out.println("Now the date is :=>  " + dateNow);
    for (int i = 0; i < foodItems.size(); i++) {
        food = foodItems.get(i);
        System.out.println("ID : " + food.getFoodID());
        System.out.println("servingID : " + food.getServingID());
        System.out.println("Name : " + food.getItemName());
        System.out.println("category : " + food.getCategory());
        System.out.println("Quantity : " + food.getQuantity());
        System.out.println("calories : " + food.getCalories());
        System.out.println("fat : " + food.getFat());
        System.out.println("carbs : " + food.getCarbs());
        System.out.println("protein : " + food.getProtein());
        System.out.println("fiber : " + food.getFiber());
        System.out.println("sodium : " + food.getSodium());
        System.out.println("sugar : " + food.getSugar());
        System.out.println("cholesterol : " + food.getCholesterol());
        System.out.println(
                "------------------------------------------------------------------------------------------------");

    }

    System.out.println("Choose a meal......");
    String mealType = scan.next();
    System.out.println("Choose an item......");
    String servingID = scan.next();
    for (int j = 0; j < foodItems.size(); j++) {
        if (foodItems.get(j).getServingID() == null ? servingID == null
                : foodItems.get(j).getServingID().equals(servingID)) {
            food = foodItems.get(j);
            break;
        }

    }
    Diet diet = new CustomizedDiet();
    diet.createDiet(food, mealType, dateNow);

}

From source file:Main.java

public static void main(String[] args) throws Throwable {
    SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
    Date date1 = format.parse("08:00:01");
    Date date2 = format.parse("23:00:05");
    Date date = new Date(date2.getTime() - date1.getTime());
    System.out.println(format.format(date));
}

From source file:Main.java

public static void main(String[] args) {
    int year = 2015;
    int month = 4;

    SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");

    GregorianCalendar calendar = new GregorianCalendar(year, month, 1);

    while (calendar.get(GregorianCalendar.MONTH) == month) {
        String dateString = format.format(calendar.getTime());
        System.out.println(dateString);

        calendar.add(GregorianCalendar.DATE, 1);
    }//from   w w w.j ava  2s  .c  o  m
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyy");
    Date date1 = sdf.parse("Thu Oct 03 07:47:22 2015");
    Date date2 = sdf.parse("Mon Jul 05 08:47:22 2015");

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

    Calendar cal1 = Calendar.getInstance();
    Calendar cal2 = Calendar.getInstance();
    cal1.setTime(date1);//from  ww w  .  j  a v a 2 s  . c om
    cal2.setTime(date2);

    if (cal1.after(cal2)) {
        System.out.println("Date1 is after Date2");
    }

    if (cal1.before(cal2)) {
        System.out.println("Date1 is before Date2");
    }

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