Tuesday 22 October 2013

Date Time Conversion utility Java

Leave a Comment
In this post, i will like to share code snippet for date time manipulation in Java.

Convert hour from integer to string

Description : Converts hour from integer to String. If the hour is 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 

Description : Converts minute from integer to String. If the minute is 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 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 different in hours:minutes:seconds:miliseconds

/**
     * 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("!"));
    }




By
NOTE : – If You have Found this post Helpful, I will appreciate if you can Share it on Facebook, Twitter and Other Social Media Sites. Thanks =)

0 comments:

Post a Comment

Subscribe to our newsletter to get the latest updates to your inbox.

Your email address is safe with us!




Founder of developersnote.com, love programming and help others people. Work as Software Developer. Graduated from UiTM and continue study in Software Engineering at UTMSpace. Follow him on Twitter , or Facebook or .



Powered by Blogger.