0% found this document useful (0 votes)
78 views

Dates and Time in JAVA

This Java class contains methods to get the current date and time in different formats. It uses the Calendar and DateFormat classes to retrieve the date and time, and SimpleDateFormat to format it as strings in formats like "hh:mm:ss a" for time, "dd-MMyyyy" for date, and DateFormat.MEDIUM, DateFormat.LONG, DateFormat.FULL for different date lengths. The class provides methods like getTime(), getDateOfToday(), getMediumSizeDate(), getLongSizeDate(), and getFullSizeDate() to return date and time strings in various formats.

Uploaded by

josedgm
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views

Dates and Time in JAVA

This Java class contains methods to get the current date and time in different formats. It uses the Calendar and DateFormat classes to retrieve the date and time, and SimpleDateFormat to format it as strings in formats like "hh:mm:ss a" for time, "dd-MMyyyy" for date, and DateFormat.MEDIUM, DateFormat.LONG, DateFormat.FULL for different date lengths. The class provides methods like getTime(), getDateOfToday(), getMediumSizeDate(), getLongSizeDate(), and getFullSizeDate() to return date and time strings in various formats.

Uploaded by

josedgm
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Getting dates and time in JAVA

By utilities4java.com

This class have some methods to get the current time and date in differents formats. /* * Document: UtilDateTime.java * Description: Class with some usefull Date and Time treatment methods * Created on: April 2010 * */ import java.util.Calendar; import java.util.Date; import java.util.Locale; import java.text.SimpleDateFormat; import java.text.DateFormat; /** * * @author Luisa Escalona */ public class UtilDateTime { /** Default Constructor */ public UtilDateTime() { } /*** Operation getTime * @return String with the current time. Formated as hh:mm:ss a ***/ public static String getTime(){ Calendar calendar = Calendar.getInstance(); Date today = calendar.getTime(); SimpleDateFormat timeFormat = new SimpleDateFormat("hh:mm:ss a"); return timeFormat.format(today); } /*** Operation getDateOfToday * @return String with the date of the current day. MM-YYYY ***/ Formated as DD-

public static String getDateOfToday(){ Calendar calendar = Calendar.getInstance(); Date today = calendar.getTime(); SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMyyyy"); return dateFormat.format(today); } /*** Operation getMediumSizeDate * @return String with the date of the current day. ***/ public static String getMediumSizeDate(){ Calendar calendar = Calendar.getInstance(); Date today = calendar.getTime(); Locale loc = new Locale("es"); // If you want spanish format //Locale loc = new Locale("it"); // If you want italian format //Locale loc = new Locale("us"); // If you want american format DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, loc); return dateFormat.format(today); } /*** Operation getLongSizeDate * @return String with the date of the current day ***/ public static String getLongSizeDate(){ Calendar calendar = Calendar.getInstance(); Date today = calendar.getTime(); Locale loc = new Locale("es"); // If you want spanish format //Locale loc = new Locale("it"); // If you want italian format //Locale loc = new Locale("us"); // If you want american format DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG, loc); return dateFormat.format(today); } /*** Operation getFullSizeDate * @return String with the date of the current day ***/ public static String getFullSizeDate(){ Calendar calendar = Calendar.getInstance(); Date today = calendar.getTime(); Locale loc = new Locale("es"); // If you want spanish format //Locale loc = new Locale("it"); // If you want italian

format //Locale loc = new Locale("us"); // If you want american format DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.FULL, loc); return dateFormat.format(today); } }

You might also like