Date Time Conversion utility Java

In this post, I will like to share a sample code snippet for date-time manipulation in Java.

Convert hour from integer to string

Scenario: Converts hour from integer to String. If the hour is a single digit, the leading zero will be added.

public static String getHourString(int localHourNumber) {
        if ((localHourNumber > 23) || (localHourNumber < 0)) {
            return "";
        }

        if ((localHourNumber >= 0) && (localHourNumber <= 9)) {
            return "0" + localHourNumber;
        }

        return String.valueOf(localHourNumber);
    }

Convert minute from integer to string 

Scenario: Converts minute from integer to String. If the minute is a single digit, the leading zero will be added.

public static String getMinuteString(int localMinuteNumber) {
        if ((localMinuteNumber > 59) || (localMinuteNumber < 0)) {
            return "";
        }

        if ((localMinuteNumber >= 0) && (localMinuteNumber <= 9)) {
            return "0" + localMinuteNumber;
        }

        return String.valueOf(localMinuteNumber);
    }

Converts the input parameter to the date format

public static String convertDateTimeFormat(String inputDate, String dateFormat) {
        String outPutDate = "";
        SimpleDateFormat patternDateTimeFormat = new SimpleDateFormat(dateFormat);
        try {
            if ((inputDate != null) && (inputDate.trim().length() > 0)) {
                // outPutDate = patternDateTimeFormat.format(new java.util.Date(inputDate));
                outPutDate = patternDateTimeFormat.format(DateFormat.getInstance().parse(inputDate));

            } else {
                outPutDate = patternDateTimeFormat.format(new java.util.Date());
            }
        } catch (Exception e) {
            outPutDate = patternDateTimeFormat.format(new java.util.Date());
        }
        return outPutDate;
    }



Gets the format java.sql.Date based on the frag ID value.

 public static java.sql.Date getSQLDateFromFragIDString(String fragID) {
        java.sql.Date date = null;
        Calendar cal = Calendar.getInstance();

        if (fragID.length() == 6) {
            cal.set(Integer.parseInt(fragID.substring(0, 4)), Integer.parseInt(fragID.substring(4, 6)) - 1, 01);
            date = new java.sql.Date(cal.getTime().getTime());
        }

        return date;
    }



Converts the input date to the date format specified Timestamp format.

/* * @param inputDate The string contains input date in the dd/MM/yyyy format.
     * @param inputTime The strin contains input time in the hh:mm:ss.fffffffff
     * format.
     * @return Timestamp Contains a Timestamp.

*/

public static Timestamp convertToTimestamp(String inputDate, String inputTime) {
        StringTokenizer stringTokenizer = new StringTokenizer(inputDate, "/");
        String day = stringTokenizer.nextToken();
        String month = stringTokenizer.nextToken();
        String year = stringTokenizer.nextToken();
        Timestamp inputTransactionDateTime = Timestamp.valueOf(year + "-" + month + "-" + day + " " + inputTime);

        return inputTransactionDateTime;
    }



Gets the previous month's value based on the entered fragmentation ID.

/**
     * Gets the previous month value based on the entered fragmentation ID.
     *
     * @param localfragId The int contains fragmentation ID to act as the base
     * for subtraction.
     * @return int Contains previous month value based on current fragmentation
     * id.
     */
    public static int getPreviousMonth(int localfragId) {

        if ((localfragId % 100) == 1) {
            localfragId = (((localfragId / 100) - 1) * 100) + 12;
        } else {
            localfragId--;
        }

        return localfragId;

    }



Gets the time difference in hours:minutes:seconds: milliseconds

/**
     * Gets the time different in hours:minutes:seconds:miliseconds.
     *
     * @param startTime The object contains the fragmentation ID to act as the
     * base for subtraction.
     * @param endTime The object contains end time.
     * @return String Contains month value based on current fragmentation id.
     */
    public static String getTimeDifferent(Calendar startTime, Calendar endTime) {

        long timeStamp = endTime.getTime().getTime() - startTime.getTime().getTime();    
        return (timeStamp / hour) + ":" + (timeStamp / minute) + ":" + (timeStamp / second) + ":" + (timeStamp / milisecond);

    }

Bonus: Method to Trims the session ID

/**
     * Trims the session ID to the full of the session ID string before the "!"
     * character.
     *
     * @param sessionID The string contains session id.
     * @return String Contains the trimmed sessionID.
     */

public static String trimSessionID(String sessionID) {
        return sessionID.substring(0, sessionID.indexOf("!"));
    }


Popular posts from this blog

How to create zip file to download in JSP- Servlet

How to create DataGrid or GridView in JSP - Servlet

Pinging in ASP.NET Example