Dates and Time in JAVA
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); } }