Example usage for java.util Date getHours

List of usage examples for java.util Date getHours

Introduction

In this page you can find the example usage for java.util Date getHours.

Prototype

@Deprecated
public int getHours() 

Source Link

Document

Returns the hour represented by this Date object.

Usage

From source file:org.nuclos.common2.DateUtils.java

/**
 * @param date is not changed by this method. May be <code>null</code>.
 * @return the pure date (aka "day", that is the date without time of day) of the given Date object (if any).
 * @postcondition date != null <--> result != null
 * @postcondition date != null --> result.getHours() == 0
 * @postcondition date != null --> result.getMinutes() == 0
 * @postcondition date != null --> result.getSeconds() == 0
 * @todo consider calling this method getDay(). On the other hand, that name could be confused with Date.getDay()...
 *//*from   ww  w .jav a 2 s . c o  m*/
public static Date getPureDate(Date date) {
    final Date result = (date == null) ? null
            : org.apache.commons.lang.time.DateUtils.truncate(date, Calendar.DATE);
    assert (date != null) == (result != null);

    assert (date == null) || result.getHours() == 0;
    assert (date == null) || result.getMinutes() == 0;
    assert (date == null) || result.getSeconds() == 0;
    return result;
}

From source file:controllers.CNBCProxy.java

private static String CalculateDateFormat(long millis) {
    // Set helping arrays
    String[] monthArr = new String[12];
    String[] dayArr = new String[7];

    // Set month names
    monthArr[0] = "January";
    monthArr[1] = "February";
    monthArr[2] = "March";
    monthArr[3] = "April";
    monthArr[4] = "May";
    monthArr[5] = "June";
    monthArr[6] = "July";
    monthArr[7] = "August";
    monthArr[8] = "September";
    monthArr[9] = "October";
    monthArr[10] = "November";
    monthArr[11] = "December";

    // Set day names
    dayArr[0] = "Sunday";
    dayArr[1] = "Monday";
    dayArr[2] = "Tuesday";
    dayArr[3] = "Wednesday";
    dayArr[4] = "Thursday";
    dayArr[5] = "Friday";
    dayArr[6] = "Saturday";

    /** Create Date object with random date, in order to calculate date of article like 
    the "setTime()" function in Javascript (date since January 1, 1970 00:00:00 gmt)
    **//*from  w ww .j av  a2s  .com*/

    Date dateObj = new Date(92, 1, 10);

    // Add long number to get article's date
    dateObj.setTime(millis);

    StringBuilder date = new StringBuilder();
    date.append(dayArr[dateObj.getDay()]).append(", ").append(Integer.toString(dateObj.getDate())).append(" ")
            .append(monthArr[dateObj.getMonth()]).append(" ").append(Integer.toString(dateObj.getYear() + 1900))
            .append(" "); // 1900 added to year(see getYear() method's documentation)

    String hours;
    String midnight;

    if (dateObj.getHours() > 12) {
        if (dateObj.getHours() - 12 < 10) {
            hours = "0" + Integer.toString(dateObj.getHours() - 12);
        } else {
            hours = Integer.toString(dateObj.getHours() - 12);
        }
        midnight = "PM";
    } else {
        if (dateObj.getHours() < 10) {
            hours = "0" + Integer.toString(dateObj.getHours());
        } else {
            hours = Integer.toString(dateObj.getHours());
        }
        midnight = "AM";
    }

    date.append(hours).append(":").append(Integer.toString(dateObj.getMinutes()))
            .append(" " + midnight + " ET");

    return date.toString();
}

From source file:org.nerve.utils.StringUtils.java

/**
 * ???/*ww w . j a  v  a 2s .  com*/
 *
 * @param time      
 * @return
 */
public static String processTime(Long time) {
    long oneDay = 24 * 60 * 60 * 1000;
    Date now = new Date();
    Date orginalTime = new Date(time);
    long nowDay = now.getTime() - (now.getHours() * 3600 + now.getMinutes() * 60 + now.getSeconds()) * 1000;
    long yesterday = nowDay - oneDay;
    String nowHourAndMinute = toDoubleDigit(orginalTime.getHours()) + ":"
            + toDoubleDigit(orginalTime.getMinutes());
    if (time >= now.getTime()) {
        return "";
    } else if ((now.getTime() - time) < (60 * 1000)) {
        return (now.getTime() - time) / 1000 + "? " + nowHourAndMinute + " ";
    } else if ((now.getTime() - time) < (60 * 60 * 1000)) {
        return (now.getTime() - time) / 1000 / 60 + "? " + nowHourAndMinute + " ";
    } else if ((now.getTime() - time) < (24 * 60 * 60 * 1000)) {
        return (now.getTime() - time) / 1000 / 60 / 60 + "?? " + nowHourAndMinute + " ";
    } else if (time >= nowDay) {
        return " " + nowHourAndMinute;
    } else if (time >= yesterday) {
        return " " + nowHourAndMinute;
    } else {
        return toDoubleDigit(orginalTime.getMonth()) + "-" + toDoubleDigit(orginalTime.getDate()) + " "
                + nowHourAndMinute + ":" + toDoubleDigit(orginalTime.getSeconds());
    }
}

From source file:org.infoscoop.request.filter.rss.RssHandler.java

/**
 * Return the date whose form is "yyyy/MM/dd HH:mm:ss" -> obsolute
 * javascript Date :An argument form of the object instance.
 * Return the date whose form is "yyyy,MM,dd,HH,mm,ss".
 * /*from  ww  w.  jav a2 s.  c o  m*/
 * @param date
 * @return
 */
public static String getFullDate(Date date) {
    if (date == null) {
        return null;
    }

    try {
        //         return formatFullDate.format(date);
        return (date.getYear() + 1900) + "," + date.getMonth() + "," + date.getDate() + "," + date.getHours()
                + "," + date.getMinutes() + "," + date.getSeconds();
    } catch (IllegalArgumentException e1) {
        return null;
    }
}

From source file:eionet.meta.exports.ods.Ods.java

/**
 * Returns date./*from w w w .  j ava 2  s. co  m*/
 *
 * @param timestamp
 *            time stamp
 *
 * @return date as a string
 */
public static String getDate(long timestamp) {

    Date date = new Date(timestamp);
    String year = String.valueOf(1900 + date.getYear());
    String month = String.valueOf(date.getMonth() + 1);
    month = (month.length() < 2) ? ("0" + month) : month;
    String day = String.valueOf(date.getDate());
    day = (day.length() < 2) ? ("0" + day) : day;
    String hours = String.valueOf(date.getHours());
    hours = (hours.length() < 2) ? ("0" + hours) : hours;
    String minutes = String.valueOf(date.getMinutes());
    minutes = (minutes.length() < 2) ? ("0" + minutes) : minutes;
    String seconds = String.valueOf(date.getSeconds());
    seconds = (seconds.length() < 2) ? ("0" + seconds) : seconds;

    String time = year;
    time = time + "-" + month;
    time = time + "-" + day;
    time = time + "T" + hours;
    time = time + ":" + minutes;
    time = time + ":" + seconds;

    return time;
}

From source file:org.metawatch.manager.Monitors.java

private static synchronized void updateWeatherDataWunderground(Context context) {
    try {//from  w  ww .  j a va  2  s .co  m

        if (WeatherData.updating)
            return;

        long currentTime = System.currentTimeMillis();

        // Prevent weather updating more frequently than every 5 mins
        if (WeatherData.timeStamp != 0 && WeatherData.received) {

            long diff = currentTime - WeatherData.timeStamp;
            if (diff < 5 * 60 * 1000) {
                if (Preferences.logging)
                    Log.d(MetaWatch.TAG, "Skipping weather update - updated less than 5m ago");
                Idle.updateIdle(context, true);
                return;
            }
        }

        WeatherData.updating = true;

        if (Preferences.logging)
            Log.d(MetaWatch.TAG, "Monitors.updateWeatherDataWunderground(): start");

        if ((LocationData.received || (Preferences.weatherGeolocation == false))
                && Preferences.wundergroundKey != "") {

            String weatherLocation = Double.toString(LocationData.latitude) + ","
                    + Double.toString(LocationData.longitude);
            if (Preferences.weatherGeolocation == false)
                weatherLocation = Preferences.weatherCity.replace(",", "").replace(" ", "%20");

            String forecastQuery = "";
            boolean hasForecast = false;

            long diff = currentTime - WeatherData.forecastTimeStamp;
            if (WeatherData.forecast == null || (diff > 3 * 60 * 60 * 1000)) {
                // Only update forecast every three hours
                forecastQuery = "forecast10day/astronomy/";
                hasForecast = true;
            }

            String requestUrl = "http://api.wunderground.com/api/" + Preferences.wundergroundKey
                    + "/geolookup/conditions/" + forecastQuery + "q/" + weatherLocation + ".json";

            if (Preferences.logging)
                Log.d(MetaWatch.TAG, "Request: " + requestUrl);

            JSONObject json = getJSONfromURL(requestUrl);

            JSONObject location = json.getJSONObject("location");
            JSONObject current = json.getJSONObject("current_observation");

            if (hasForecast) {
                JSONObject moon = json.getJSONObject("moon_phase");
                JSONObject sunrise = moon.getJSONObject("sunrise");
                WeatherData.sunriseH = sunrise.getInt("hour");
                WeatherData.sunriseM = sunrise.getInt("minute");
                JSONObject sunset = moon.getJSONObject("sunset");
                WeatherData.sunsetH = sunset.getInt("hour");
                WeatherData.sunsetM = sunset.getInt("minute");

                WeatherData.moonPercentIlluminated = moon.getInt("percentIlluminated");
                WeatherData.ageOfMoon = moon.getInt("ageOfMoon");
            }

            boolean isDay = true;

            Date dt = new Date();
            int hours = dt.getHours();
            int minutes = dt.getMinutes();

            if ((hours < WeatherData.sunriseH)
                    || (hours == WeatherData.sunriseH && minutes < WeatherData.sunriseM)
                    || (hours > WeatherData.sunsetH)
                    || (hours == WeatherData.sunsetH && minutes > WeatherData.sunsetM)) {
                isDay = false;
            }

            WeatherData.locationName = location.getString("city");
            WeatherData.condition = current.getString("weather");
            WeatherData.icon = getIconWunderground(current.getString("icon"), isDay);

            if (Preferences.weatherCelsius) {
                WeatherData.temp = current.getString("temp_c");
            } else {
                WeatherData.temp = current.getString("temp_f");
            }

            if (hasForecast) {
                JSONObject forecast = json.getJSONObject("forecast");
                JSONArray forecastday = forecast.getJSONObject("simpleforecast").getJSONArray("forecastday");

                int days = forecastday.length();
                WeatherData.forecast = new Forecast[days];

                for (int i = 0; i < days; ++i) {
                    WeatherData.forecast[i] = m.new Forecast();
                    JSONObject day = forecastday.getJSONObject(i);
                    JSONObject date = day.getJSONObject("date");

                    WeatherData.forecast[i].icon = getIconWunderground(day.getString("icon"), true);
                    WeatherData.forecast[i].day = date.getString("weekday_short");
                    if (Preferences.weatherCelsius) {
                        WeatherData.forecast[i].tempLow = day.getJSONObject("low").getString("celsius");
                        WeatherData.forecast[i].tempHigh = day.getJSONObject("high").getString("celsius");
                    } else {
                        WeatherData.forecast[i].tempLow = day.getJSONObject("low").getString("fahrenheit");
                        WeatherData.forecast[i].tempHigh = day.getJSONObject("high").getString("fahrenheit");
                    }
                }

                WeatherData.forecastTimeStamp = System.currentTimeMillis();
            }

            WeatherData.celsius = Preferences.weatherCelsius;

            WeatherData.received = true;

            Idle.updateIdle(context, true);
            MetaWatchService.notifyClients();
            WeatherData.timeStamp = System.currentTimeMillis();
        }

    } catch (Exception e) {
        if (Preferences.logging)
            Log.e(MetaWatch.TAG, "Exception while retreiving weather", e);
    } finally {
        if (Preferences.logging)
            Log.d(MetaWatch.TAG, "Monitors.updateWeatherData(): finish");

        WeatherData.updating = false;
    }
}

From source file:org.sonar.server.filters.DateCriterionTest.java

@Test
public void ignoreTime() {
    DateCriterion criterion = new DateCriterion().setDate(3);
    Date date = criterion.getDate();
    assertThat(date.getHours(), is(0));
    assertThat(date.getMinutes(), is(0));
}

From source file:com.yj.smarthome.activity.control.MainControlActivity.java

/**
 * ??2014624 17:23./*from w  w w  .  ja va  2 s.  c om*/
 * 
 * @param date
 *            the date
 * @return the date cn
 */
public static String getDateCN(Date date) {
    int y = date.getYear();
    int m = date.getMonth() + 1;
    int d = date.getDate();
    int h = date.getHours();
    int mt = date.getMinutes();
    return (y + 1900) + "" + m + "" + d + "  " + h + ":" + mt;
}

From source file:org.sonar.server.filters.DateCriterionTest.java

@Test
public void testDaysAgo() {
    DateCriterion criterion = new DateCriterion().setDate(3);
    Date date = criterion.getDate();
    assertThat(date.getMinutes(), is(0));
    assertThat(date.getHours(), is(0));
    assertThat(DateUtils.isSameDay(date, DateUtils.addDays(new Date(), -3)), is(true));
}

From source file:com.krawler.esp.servlets.importICSServlet.java

public static String getTimeHourDiff(String Estartts, String Eendts) {
    long hours = 0;
    long diffInMilleseconds = 0;
    try {/*from   w  ww  . j a va  2s  .c  om*/
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:00:00.00");

        if (Estartts.compareTo("") != 0 && Eendts.compareTo("") != 0) {
            java.util.Date sdt = sdf.parse(Estartts);
            java.util.Date edt = sdf.parse(Eendts);

            diffInMilleseconds = edt.getTime() - sdt.getTime();
            hours = diffInMilleseconds / (60 * 60 * 1000);
            if (hours < 1) {
                if (edt.getHours() == 0) {
                    Eendts = getNextDay(Estartts);
                } else {
                    diffInMilleseconds = edt.getTime() + (60 * 60 * 1000);
                    edt.setTime(diffInMilleseconds);
                    Eendts = sdf.format(edt);
                }
            }
        }
    } catch (ParseException ex) {
        hours = 0;
    } finally {
        return Eendts;
    }
}