Format Date Time In Android

Here I am going to show you some useful methods which you will need in your applications. In every application we need to get the date and time in some format and there will be lot of confusion for developers by parsing date time. Here is the solution for all of them.

  • Current Local time Millisecs

    This will return the current local time in millisecs.
    1. public static long getCurrentLocalTimeLong()  
    2. {  
    3.     SimpleDateFormat timeFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm", Locale.US);  
    4.     Calendar calender = Calendar.getInstance();  
    5.     TimeZone currentTimeZone = calender.getTimeZone();  
    6.     timeFormat.setTimeZone(currentTimeZone);  
    7.     String current_time = timeFormat.format(new Date());  
    8.     Date currentTimeMillSecs = null;  
    9.     try  
    10.     {  
    11.         currentTimeMillSecs = timeFormat.parse(current_time);  
    12.     } catch (ParseException e) {} // new  
    13.     return currentTimeMillSecs.getTime();  
    14. }  
  • Today mid night time in specific format

    This will return the end time of today as string.
    1. public static String getTodayMidNight()  
    2. {  
    3.     Calendar cal = Calendar.getInstance();  
    4.     Date date = cal.getTime();  
    5.     SimpleDateFormat sdf = new SimpleDateFormat(  
    6.         "dd-MM-yyyy HH:mm:ss", Locale.US);  
    7.     cal.set(Calendar.HOUR_OF_DAY, 23); // set hours to  
    8.     // zero  
    9.     cal.set(Calendar.MINUTE, 59); // set minutes to zero  
    10.     cal.set(Calendar.SECOND, 00); // set seconds to zero  
    11.     cal.add(Calendar.DATE, 00);  
    12.     date = cal.getTime();  
    13.     String endTime = sdf.format(date);  
    14.   
    15.   
    16.     Date startDate = null;  
    17.     try   
    18.     {  
    19.         startDate = sdf.parse(endTime);  
    20.     } catch (Exception ex)  
    21.     {  
    22.         ex.printStackTrace();  
    23.     }  
    24.   
    25.   
    26.     return String.valueOf(startDate.getTime());  
    27. }  
  • Checking weather the time is expired or not

    This function will check if the given tim in millisecs is less than the current time or not.
    1. public static boolean isTimeExpired(long expiryTime)  
    2. {  
    3.     try  
    4.     {  
    5.         Calendar lCDateTime = Calendar.getInstance();  
    6.         long currentTime = lCDateTime.getTimeInMillis();  
    7.         if (expiryTime < currentTime)  
    8.             return true;  
    9.         return false;  
    10.     } catch (Exception e)   
    11.     {  
    12.         return false;  
    13.     }  
    14. }  
  • Today date

    This function will return today's date in the specified format.
    1. public static String today()   
    2. {  
    3.     Calendar cal = Calendar.getInstance();  
    4.     Date date = cal.getTime();  
    5.     SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy", Locale.US);  
    6.     return sdf.format(date);  
    7. }  
  • Now

    This will return date and time now in specified format.
    1. public static String now()   
    2. {  
    3.     Calendar cal = Calendar.getInstance();  
    4.     Date date = cal.getTime();  
    5.     SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss", Locale.US);  
    6.     return sdf.format(date);  
    7. }  
  • Get date and time in specified format

    This will return the date and time in specified format.
    1. public static String getDateFromLongTapFormat(String longDate)   
    2. {  
    3.     String returnDate = null;  
    4.     try  
    5.     {  
    6.         SimpleDateFormat timeFormat = new SimpleDateFormat(  
    7.             AppSettings.TAP_DATE_FORMAT, Locale.US);  
    8.         Calendar calender = Calendar.getInstance();  
    9.         TimeZone currentTimeZone = calender.getTimeZone();  
    10.         timeFormat.setTimeZone(currentTimeZone);  
    11.         Date date = new Date(Long.parseLong(longDate));  
    12.         returnDate = timeFormat.format(date);  
    13.     } catch (Exception ex)   
    14.     {  
    15.         returnDate = longDate;  
    16.     }  
    17.     return returnDate;  
    18. }  
Ebook Download
View all
Learn
View all