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.
- public static long getCurrentLocalTimeLong()
- {
- SimpleDateFormat timeFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm", Locale.US);
- Calendar calender = Calendar.getInstance();
- TimeZone currentTimeZone = calender.getTimeZone();
- timeFormat.setTimeZone(currentTimeZone);
- String current_time = timeFormat.format(new Date());
- Date currentTimeMillSecs = null;
- try
- {
- currentTimeMillSecs = timeFormat.parse(current_time);
- } catch (ParseException e) {}
- return currentTimeMillSecs.getTime();
- }
- Today mid night time in specific format
This will return the end time of today as string.
- public static String getTodayMidNight()
- {
- Calendar cal = Calendar.getInstance();
- Date date = cal.getTime();
- SimpleDateFormat sdf = new SimpleDateFormat(
- "dd-MM-yyyy HH:mm:ss", Locale.US);
- cal.set(Calendar.HOUR_OF_DAY, 23);
-
- cal.set(Calendar.MINUTE, 59);
- cal.set(Calendar.SECOND, 00);
- cal.add(Calendar.DATE, 00);
- date = cal.getTime();
- String endTime = sdf.format(date);
-
-
- Date startDate = null;
- try
- {
- startDate = sdf.parse(endTime);
- } catch (Exception ex)
- {
- ex.printStackTrace();
- }
-
-
- return String.valueOf(startDate.getTime());
- }
- 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.
- public static boolean isTimeExpired(long expiryTime)
- {
- try
- {
- Calendar lCDateTime = Calendar.getInstance();
- long currentTime = lCDateTime.getTimeInMillis();
- if (expiryTime < currentTime)
- return true;
- return false;
- } catch (Exception e)
- {
- return false;
- }
- }
- Today date
This function will return today's date in the specified format.
- public static String today()
- {
- Calendar cal = Calendar.getInstance();
- Date date = cal.getTime();
- SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy", Locale.US);
- return sdf.format(date);
- }
- Now
This will return date and time now in specified format.
- public static String now()
- {
- Calendar cal = Calendar.getInstance();
- Date date = cal.getTime();
- SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss", Locale.US);
- return sdf.format(date);
- }
- Get date and time in specified format
This will return the date and time in specified format.
- public static String getDateFromLongTapFormat(String longDate)
- {
- String returnDate = null;
- try
- {
- SimpleDateFormat timeFormat = new SimpleDateFormat(
- AppSettings.TAP_DATE_FORMAT, Locale.US);
- Calendar calender = Calendar.getInstance();
- TimeZone currentTimeZone = calender.getTimeZone();
- timeFormat.setTimeZone(currentTimeZone);
- Date date = new Date(Long.parseLong(longDate));
- returnDate = timeFormat.format(date);
- } catch (Exception ex)
- {
- returnDate = longDate;
- }
- return returnDate;
- }