Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Problem Description: How to format time in AM-PM format? Solution: This example formats the time by using SimpleDateFormat("HH-mm-ss a") constructor and sdf.format(date) method of SimpleDateFormat class. import java.text.SimpleDateFormat; import java.util.Date; public class Main{ 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)); } } Result: The above code sample will produce the following result.The result will change depending upon the current system time 06:40:32 AM Problem Description: How to display name of a month in (MMM) format ? Solution: This example shows how to display the current month in the (MMM) format with the help of Calender.getInstance() method of Calender class and fmt.format() method of Formatter class. import java.util.Calendar; import java.util.Formatter; public class MainClass{ public static void main(String args[]){ Formatter fmt = new Formatter(); Calendar cal = Calendar.getInstance(); fmt = new Formatter(); fmt.format("%tB %tb %tm", cal, cal, cal); System.out.println(fmt); } } Result: The above code sample will produce the following result. October Oct 10 Problem Description: How to display hour and minute ? Solution: This example demonstrates how to display the hour and minute of that moment by using Calender.getInstance() of Calender class. import java.util.Calendar; import java.util.Formatter; public class MainClass{ public static void main(String args[]){ Formatter fmt = new Formatter(); Calendar cal = Calendar.getInstance(); fmt = new Formatter(); fmt.format("%tl:%tM", cal, cal); System.out.println(fmt); } } Result: The above code sample will produce the following result.The result will chenge depending upon the current system time. 03:20 Problem Description: How to display current date and time? Solution: This example shows how to display the current date and time using Calender.getInstance() method of Calender class and fmt.format() method of Formatter class. import java.util.Calendar; import java.util.Formatter; public class MainClass{ public static void main(String args[]){ Formatter fmt = new Formatter(); Calendar cal = Calendar.getInstance(); fmt = new Formatter(); fmt.format("%tc", cal); System.out.println(fmt); } } Result: The above code sample will produce the following result.The result will change depending upon the current system date and time Wed Oct 15 20:37:30 GMT+05:30 2008 Problem Description: How to format time in 24 hour format? Solution: This example formats the time into 24 hour format (00:00-24:00) by using sdf.format(date) method of SimpleDateFormat class. import java.text.SimpleDateFormat; import java.util.Date; public class Main { public static void main(String[] args) { Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("h"); System.out.println("hour in h format : " + sdf.format(date)); } } Result: The above code sample will produce the following result. hour in h format : 8 Problem Description: How to format time in MMMM format? Solution: This example formats the month with the help of SimpleDateFormat(�MMMM�) constructor and sdf.format(date) method of SimpleDateFormat class. import java.text.SimpleDateFormat; import java.util.Date; public class Main{ public static void main(String[] args){ Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("MMMM"); System.out.println("Current Month in MMMM format : " + sdf.format(date)); } } Result: The above code sample will produce the following result.The result will change depending upon the current system date Current Month in MMMM format : May Problem Description: How to format seconds? Solution: This example formats the second by using SimpleDateFormat(�ss�) constructor and sdf.format(date) method of SimpleDateFormat class. import java.text.SimpleDateFormat; import java.util.Date; public class Main{ public static void main(String[] args){ Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("ss"); System.out.println("seconds in ss format : " + sdf.format(date)); } } Result: The above code sample will produce the following result.The result will change depending upon the current system time. seconds in ss format : 14 Problem Description: How to display name of the months in short format? Solution: This example displays the names of the months in short form with the help of DateFormatSymbols().getShortMonths() method of DateFormatSymbols class. import java.text.SimpleDateFormat; import java.text.DateFormatSymbols; public class Main { public static void main(String[] args) { String[] shortMonths = new DateFormatSymbols() .getShortMonths(); for (int i = 0; i < (shortMonths.length-1); i++) { String shortMonth = shortMonths[i]; System.out.println("shortMonth = " + shortMonth); } } } Result: The above code sample will produce the following result. shortMonth shortMonth shortMonth shortMonth shortMonth shortMonth shortMonth shortMonth shortMonth shortMonth shortMonth shortMonth = = = = = = = = = = = = Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec Problem Description: How to display name of the weekdays ? Solution: This example displays the names of the weekdays in short form with the help of DateFormatSymbols().getWeekdays() method of DateFormatSymbols class. import java.text.SimpleDateFormat; import java.text.DateFormatSymbols; public class Main { public static void main(String[] args) { String[] weekdays = new DateFormatSymbols().getWeekdays(); for (int i = 2; i < (weekdays.length-1); i++) { String weekday = weekdays[i]; System.out.println("weekday = " + weekday); } } } Result: The above code sample will produce the following result. weekday weekday weekday weekday weekday = = = = = Monday Tuesday Wednesday Thursday Friday Problem Description: How to add time(Days, years , seconds) to Date? Solution: The following examples shows us how to add time to a date using add() method of Calender. import java.util.*; public class Main { public static void main(String[] args) throws Exception { Date d1 = new Date(); Calendar cl = Calendar. getInstance(); cl.setTime(d1); System.out.println("today is " + d1.toString()); cl. add(Calendar.MONTH, 1); System.out.println("date after a month will be " + cl.getTime().toString() ); cl. add(Calendar.HOUR, 70); System.out.println("date after 7 hrs will be " + cl.getTime().toString() ); cl. add(Calendar.YEAR, 3); System.out.println("date after 3 years will be " + cl.getTime().toString() ); } } Result: The above code sample will produce the following result. today is Mon date after a date after 7 date after 3 Jun 22 02:47:02 IST 2009 month will be Wed Jul 22 02:47:02 IST 2009 hrs will be Wed Jul 22 09:47:02 IST 2009 years will be Sun Jul 22 09:47:02 IST 2012 Problem Description: How to display time in different country's format? Solution: Following example uses Locale class & DateFormat class to disply date in different Country's format. import java.text.DateFormat; import java.util.*; public class Main { public static void main(String[] args) throws Exception { Date d1 = new Date(); System.out.println("today is "+ d1.toString()); Locale locItalian = new Locale("it","ch"); DateFormat df = DateFormat.getDateInstance (DateFormat.FULL, locItalian); System.out.println("today is in Italian Language in Switzerland Format : "+ df.format(d1)); } } Result: The above code sample will produce the following result. today is Mon Jun 22 02:37:06 IST 2009 today is in Italian Language in Switzerlandluned�, 22. giugno 2009 Problem Description: How to display time in different languages? Solution: This example uses DateFormat class to display time in Italian. import java.text.DateFormat; import java.util.*; public class Main { public static void main(String[] args) throws Exception { Date d1 = new Date(); System.out.println("today is "+ d1.toString()); Locale locItalian = new Locale("it"); DateFormat df = DateFormat.getDateInstance (DateFormat.FULL, locItalian); System.out.println("today is "+ df.format(d1)); } } Result: The above code sample will produce the following result. today is Mon Jun 22 02:33:27 IST 2009 today is luned� 22 giugno 2009 Problem Description: How to roll through hours & months? Solution: This example shows us how to roll through monrhs (without changing year) or hrs(without changing month or year) using roll() method of Class calender. import java.util.*; public class Main { public static void main(String[] args) throws Exception { Date d1 = new Date(); Calendar cl = Calendar. getInstance(); cl.setTime(d1); System.out.println("today is "+ d1.toString()); cl. roll(Calendar.MONTH, 100); System.out.println("date after a month will be " + cl.getTime().toString() ); cl. roll(Calendar.HOUR, 70); System.out.println("date after 7 hrs will be " + cl.getTime().toString() ); } } Result: The above code sample will produce the following result. today is Mon Jun 22 02:44:36 IST 2009 date after a month will be Thu Oct 22 02:44:36 IST 2009 date after 7 hrs will be Thu Oct 22 00:44:36 IST 2009 Problem Description: How to find which week of the year, month? Solution: The following example displays week no of the year & month. import java.util.*; public class Main { public static void main(String[] args) throws Exception { Date d1 = new Date(); Calendar cl = Calendar. getInstance(); cl.setTime(d1); System.out.println("today is " + cl.WEEK_OF_YEAR+ " week of the year"); System.out.println("today is a "+cl.DAY_OF_MONTH + "month of the year"); System.out.println("today is a "+cl.WEEK_OF_MONTH +"week of the month"); } } Result: The above code sample will produce the following result. today is 30 week of the year today is a 5month of the year today is a 4week of the month Problem Description: How to display date in different formats ? Solution: This example displays the names of the weekdays in short form with the help of DateFormatSymbols().getWeekdays() method of DateFormatSymbols class. import java.text.*; import java.util.*; public class Main { public static void main(String[] args) { Date dt = new Date(1000000000000L); DateFormat[] dtformat = new DateFormat[6]; dtformat[0] = DateFormat.getInstance(); dtformat[1] = DateFormat.getDateInstance(); dtformat[2] = DateFormat.getDateInstance(DateFormat.MEDIUM); dtformat[3] = DateFormat.getDateInstance(DateFormat.FULL); dtformat[4] = DateFormat.getDateInstance(DateFormat.LONG); dtformat[5] = DateFormat.getDateInstance(DateFormat.SHORT); for(DateFormat dateform : dtformat) System.out.println(dateform.format(dt)); } } Result: The above code sample will produce the following result. 9/9/01 7:16 AM Sep 9, 2001 Sep 9, 2001 Sunday, September 9, 2001 September 9, 2001 9/9/01