List of usage examples for java.text SimpleDateFormat SimpleDateFormat
public SimpleDateFormat(String pattern)
SimpleDateFormat
using the given pattern and the default date format symbols for the default java.util.Locale.Category#FORMAT FORMAT locale. From source file:Main.java
public static void main(String[] args) throws Exception { SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd"); Date workDate = simpleDateFormat1.parse("2011-11-27"); Calendar workCalendar = Calendar.getInstance(); workCalendar.setTime(workDate);//from ww w . ja v a2s . c o m SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("HH:mm:ss"); Calendar time = Calendar.getInstance(); time.setTime(simpleDateFormat2.parse("06:00:00")); workCalendar.set(Calendar.HOUR_OF_DAY, time.get(Calendar.HOUR_OF_DAY)); workCalendar.set(Calendar.MINUTE, time.get(Calendar.MINUTE)); workCalendar.set(Calendar.SECOND, time.get(Calendar.SECOND)); Date newWorkDate = workCalendar.getTime(); SimpleDateFormat simpleDateFormat3 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); System.out.println(simpleDateFormat3.format(newWorkDate)); }
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 w w w . ja v a 2 s.c o m*/ 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"); } }
From source file:Main.java
public static void main(String[] args) { Date date = new Date(); String strDateFormat = "HH:mm:ss a"; SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat); System.out.println(sdf.format(date)); }
From source file:Main.java
public static void main(String[] args) throws Exception { // Declare a date format for parsing SimpleDateFormat dateParser = new SimpleDateFormat("h:mm a"); // Parse the time string Date date = dateParser.parse("3:30 PM"); // Declare a date format for printing SimpleDateFormat dateFormater = new SimpleDateFormat("HH:mm"); // Print the previously parsed time System.out.println(dateFormater.format(date)); }
From source file:Main.java
public static void main(String[] args) { try {//from ww w.ja v a2 s.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.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[] argv) throws Exception { SimpleDateFormat formatter = new SimpleDateFormat("yy"); formatter.set2DigitYearStart(new Date()); System.out.println(formatter.format(new Date())); }
From source file:Main.java
public static void main(String[] argv) throws Exception { JFormattedTextField tft3 = new JFormattedTextField(new SimpleDateFormat("yyyy-M-d")); tft3.setValue(new Date()); Date date = (Date) tft3.getValue(); }
From source file:Main.java
public static void main(String[] args) throws ParseException { SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); long second = 1000l; long minute = 60l * second; long hour = 60l * minute; // parsing input Date date1 = dateFormat.parse("02/26/2015 09:00:00"); Date date2 = dateFormat.parse("02/26/2015 19:30:00"); // calculation long diff = date2.getTime() - date1.getTime(); // printing output System.out.print(String.format("%02d", diff / hour)); System.out.print(":"); System.out.print(String.format("%02d", (diff % hour) / minute)); System.out.print(":"); System.out.print(String.format("%02d", (diff % minute) / second)); }
From source file:Main.java
public static void main(final String[] args) throws ParseException { final SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z"); f.setTimeZone(TimeZone.getTimeZone("UTC")); System.out.println(f.format(new Date())); }
From source file:Main.java
public static void main(String args[]) { Date date = new Date(); Format formatter = new SimpleDateFormat("dd/MM/yyyy"); String s = formatter.format(date); System.out.println(s);/*from w w w. j a v a2 s. co m*/ date = new Date(); String df = DateFormat.getDateInstance().format(date); System.out.println(df); }