Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.
public Long timeDifference(String weboutput) {
    try {
        Calendar calendar = GregorianCalendar.getInstance();
        Calendar today = new GregorianCalendar();
        Date inputTime;
        if (weboutput.length() <= 10) { // for data fetched for current date.
            DateFormat formatter = new SimpleDateFormat("hh:mm:ss a", Locale.US);
            inputTime = formatter.parse(weboutput);
            calendar.setTime(inputTime);
            int hour = calendar.get(Calendar.HOUR);
            int minute = calendar.get(Calendar.MINUTE);
            int second = calendar.get(Calendar.SECOND);
            today.setTime(new Date());
            today.set(Calendar.HOUR, hour);
            today.set(Calendar.MINUTE, minute);
            today.set(Calendar.SECOND, second);
        } else {
            if (weboutput.length() <= 15) { // for data for earlier date in same year or month.
                DateFormat formatter = new SimpleDateFormat("MMM dd hh:mm a", Locale.US);
                inputTime = formatter.parse(weboutput);
                calendar.setTime(inputTime);
                int hour = calendar.get(Calendar.HOUR);
                int minute = calendar.get(Calendar.MINUTE);
                int month = calendar.get(Calendar.MONTH);
                int date = calendar.get(Calendar.DATE);
                today.setTime(new Date());
                today.set(Calendar.HOUR, hour);
                today.set(Calendar.MINUTE, minute);
                today.set(Calendar.MONTH, month);
                today.set(Calendar.DATE, date);
            } else { // for data with different year.
                DateFormat formatter = new SimpleDateFormat("MMM dd, yyyy hh:mm:ss a", Locale.US);
                inputTime = formatter.parse(weboutput);
                calendar.setTime(inputTime);
                int hour = calendar.get(Calendar.HOUR);
                int minute = calendar.get(Calendar.MINUTE);
                int second = calendar.get(Calendar.SECOND);
                int month = calendar.get(Calendar.MONTH);
                int date = calendar.get(Calendar.DATE);
                int year = calendar.get(Calendar.YEAR);
                today.setTime(new Date());
                today.set(Calendar.HOUR, hour);
                today.set(Calendar.MINUTE, minute);
                today.set(Calendar.SECOND, second);
                today.set(Calendar.YEAR, year);
                today.set(Calendar.MONTH, month);
                today.set(Calendar.DATE, date);
            }
        }
        Date retrivedDate = today.getTime();
        Calendar cal = Calendar.getInstance();
        Date currentDate = cal.getTime();
        difference = currentDate.getTime() - retrivedDate.getTime();
        System.out.println(retrivedDate);
        System.out.println(currentDate);
        System.out.println(difference);

    } catch (ParseException e) {
        e.printStackTrace();
    }
    return difference;
}

public boolean alarmValue(Long alarmTime) {
    if (alarmTime <= 1800000) // change this value for Alarm duration, currently 30 min = 30* 60 s = 1800 * 1000 ms = 1800000 ms.
        return false;
    else
        return true;
}
share|improve this question
5  
Welcome to Code Review! Please include a description at the top before your code, briefly explaining what your code does. Without that, not only it's harder to review, but the summary of your question on the front page shows up as "..." instead of something interesting. See also this meta post to make the most out of asking questions here. Enjoy the ride! –  janos 2 days ago
1  
Welcome to CR! Are you on Java 8? –  h.j.k. 2 days ago
    
A bit of background would be useful. Where does weboutput come from, and what makes you so certain that it is in one of those three formats? –  200_success 2 days ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.