Example usage for java.text DateFormat setTimeZone

List of usage examples for java.text DateFormat setTimeZone

Introduction

In this page you can find the example usage for java.text DateFormat setTimeZone.

Prototype

public void setTimeZone(TimeZone zone) 

Source Link

Document

Sets the time zone for the calendar of this DateFormat object.

Usage

From source file:it.reply.orchestrator.resource.common.AbstractResource.java

private String convertDate(Date date) {
    TimeZone tz = TimeZone.getTimeZone("UTC");
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mmZ");
    df.setTimeZone(tz);
    return df.format(date);
}

From source file:org.nodatime.tzvalidate.Java7Dump.java

@Override
public ZoneTransitions getTransitions(String id, int fromYear, int toYear) {
    TimeZone zone = TimeZone.getTimeZone(id);
    DateFormat nameFormat = new SimpleDateFormat("zzz", Locale.US);
    nameFormat.setTimeZone(zone);

    // Given the way we find transitions, we really don't want to go
    // from any earlier than this...
    if (fromYear < 1800) {
        fromYear = 1800;/*from www. j  ava 2 s . c om*/
    }

    Calendar calendar = GregorianCalendar.getInstance(UTC);
    calendar.set(fromYear, 0, 1, 0, 0, 0);
    calendar.set(Calendar.MILLISECOND, 0);
    long start = calendar.getTimeInMillis();
    calendar.set(toYear, 0, 1, 0, 0, 0);
    long end = calendar.getTimeInMillis();
    Date date = new Date(start);

    ZoneTransitions transitions = new ZoneTransitions(id);
    transitions.addTransition(null, zone.getOffset(start), zone.inDaylightTime(date), nameFormat.format(date));

    Long transition = getNextTransition(zone, start - 1, end);
    while (transition != null) {
        date.setTime(transition);
        transitions.addTransition(date, zone.getOffset(transition), zone.inDaylightTime(date),
                nameFormat.format(date));
        transition = getNextTransition(zone, transition, end);
    }
    return transitions;
}

From source file:org.elasticsearch.river.solr.support.SolrIndexer.java

public SolrIndexer(String solrUrl) {
    this.httpClient = HttpClients.createDefault();
    this.solrUrl = solrUrl + "/update";
    this.objectMapper = new ObjectMapper();
    this.objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
    dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    objectMapper.setDateFormat(dateFormat);
}

From source file:org.akvo.flow.service.TimeCheckService.java

private void checkTime() {
    if (!StatusUtil.hasDataConnection(this)) {
        Log.d(TAG, "No internet connection. Can't perform the time check.");
        return;//  w  w  w. ja  v a  2 s. c  o  m
    }

    // Since a misconfigured date/time might be considering the SSL certificate as expired,
    // we'll use HTTP by default, instead of HTTPS
    String serverBase = StatusUtil.getServerBase(this);
    if (serverBase.startsWith("https")) {
        serverBase = "http" + serverBase.substring("https".length());
    }

    try {
        final String url = serverBase + TIME_CHECK_PATH;
        String response = HttpUtil.httpGet(url);
        if (!TextUtils.isEmpty(response)) {
            JSONObject json = new JSONObject(response);
            String time = json.getString("time");
            if (!TextUtils.isEmpty(time) && !time.equalsIgnoreCase("null")) {
                DateFormat df = new SimpleDateFormat(PATTERN);
                df.setTimeZone(TimeZone.getTimeZone(TIMEZONE));
                final long remote = df.parse(time).getTime();
                final long local = System.currentTimeMillis();
                boolean onTime = Math.abs(remote - local) < OFFSET_THRESHOLD;

                if (!onTime) {
                    Intent i = new Intent(this, TimeCheckActivity.class);
                    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(i);
                }
            }
        }
    } catch (Exception e) {
        Log.e(TAG, "Error fetching time: ", e);
        PersistentUncaughtExceptionHandler.recordException(e);
    }
}

From source file:Activities.java

private String addData(String endpoint) {
    String data = null;//from w  w  w.j  a va2s . co m
    try {
        // Construct request payload
        JSONObject attrObj = new JSONObject();
        attrObj.put("name", "URL");
        attrObj.put("value", "http://www.nvidia.com/game-giveaway");

        JSONArray attrArray = new JSONArray();
        attrArray.add(attrObj);

        TimeZone tz = TimeZone.getTimeZone("UTC");
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'");
        df.setTimeZone(tz);
        String dateAsISO = df.format(new Date());

        // Required attributes
        JSONObject obj = new JSONObject();
        obj.put("leadId", "1001");
        obj.put("activityDate", dateAsISO);
        obj.put("activityTypeId", "1001");
        obj.put("primaryAttributeValue", "Game Giveaway");
        obj.put("attributes", attrArray);
        System.out.println(obj);

        // Make request
        URL url = new URL(endpoint);
        HttpsURLConnection urlConn = (HttpsURLConnection) url.openConnection();
        urlConn.setRequestMethod("POST");
        urlConn.setAllowUserInteraction(false);
        urlConn.setDoOutput(true);
        urlConn.setRequestProperty("Content-type", "application/json");
        urlConn.setRequestProperty("accept", "application/json");
        urlConn.connect();
        OutputStream os = urlConn.getOutputStream();
        os.write(obj.toJSONString().getBytes());
        os.close();

        // Inspect response
        int responseCode = urlConn.getResponseCode();
        if (responseCode == 200) {
            System.out.println("Status: 200");
            InputStream inStream = urlConn.getInputStream();
            data = convertStreamToString(inStream);
            System.out.println(data);
        } else {
            System.out.println(responseCode);
            data = "Status:" + responseCode;
        }
    } catch (MalformedURLException e) {
        System.out.println("URL not valid.");
    } catch (IOException e) {
        System.out.println("IOException: " + e.getMessage());
        e.printStackTrace();
    }

    return data;
}

From source file:hello.Scraper.java

@Transformer(inputChannel = "channel3", outputChannel = "channel4")
public DumpEntry convert(Element payload) throws ParseException {
    String dateStr = payload.ownText().substring(0, 19);

    DateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    format.setTimeZone(TimeZone.getTimeZone("GMT"));

    Date timestamp = format.parse(dateStr);

    Elements list = payload.select("a");
    String id;/*from  w  w  w.  j a v  a 2 s . c  om*/
    String ref;
    if (list.size() > 0) {
        Element a = list.get(0);
        id = a.ownText();
        ref = a.attr("href");
    } else {
        id = "private data";
        ref = null;
    }

    Element span = payload.select("span").get(0);
    String status = span.ownText();

    return new DumpEntry(timestamp, id, ref, status);
}

From source file:gov.wa.wsdot.android.wsdot.ui.FerriesRouteSchedulesDayDeparturesActivity.java

@SuppressWarnings("unchecked")
@Override//from w ww.j a  v a2s.  co m
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_ferries_route_schedules_day_departures);

    DateFormat dateFormat = new SimpleDateFormat("EEEE");
    dateFormat.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
    Bundle args = getIntent().getExtras();
    String title = args.getString("terminalNames");
    mPosition = args.getInt("position");
    mScheduleDateItems = (ArrayList<FerriesScheduleDateItem>) getIntent()
            .getSerializableExtra("scheduleDateItems");

    getSupportActionBar().setTitle(title);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    mDaysOfWeek = new ArrayList<String>();
    int numDates = mScheduleDateItems.size();
    for (int i = 0; i < numDates; i++) {
        mDaysOfWeek.add(dateFormat.format(new Date(Long.parseLong(mScheduleDateItems.get(i).getDate()))));
    }

    Context context = getSupportActionBar().getThemedContext();
    ArrayAdapter<String> list = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item,
            mDaysOfWeek);
    list.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
    getSupportActionBar().setListNavigationCallbacks(list, this);

}

From source file:com.scraper.SignedRequestsHelper.java

private String timestamp() {
    String timestamp = null;/*from   w ww  .java2s .com*/
    Calendar cal = Calendar.getInstance();
    DateFormat dfm = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
    dfm.setTimeZone(TimeZone.getTimeZone("GMT"));
    timestamp = dfm.format(cal.getTime());
    return timestamp;
}

From source file:routines.system.BigDataParserUtils.java

public synchronized static java.util.Date parseTo_Date(String s, String pattern) {
    if (isBlank(s)) {
        return null;
    }/* w  w w  . j av a  2s .c o m*/
    String s2 = s.trim();
    String pattern2 = pattern;
    if (isBlank(pattern2)) {
        pattern2 = Constant.dateDefaultPattern;
    }
    java.util.Date date = null;
    if (pattern2.equals("yyyy-MM-dd'T'HH:mm:ss'000Z'")) {
        if (!s2.endsWith("000Z")) {
            throw new RuntimeException("Unparseable date: \"" + s2 + "\""); //$NON-NLS-1$ //$NON-NLS-2$
        }
        pattern2 = "yyyy-MM-dd'T'HH:mm:ss";
        s2 = s.substring(0, s.lastIndexOf("000Z"));
    }
    DateFormat format = FastDateParser.getInstance(pattern2);
    ParsePosition pp = new ParsePosition(0);
    pp.setIndex(0);

    format.setTimeZone(TimeZone.getTimeZone("UTC"));
    date = format.parse(s2, pp);
    if (pp.getIndex() != s2.length() || date == null) {
        throw new RuntimeException("Unparseable date: \"" + s2 + "\""); //$NON-NLS-1$ //$NON-NLS-2$
    }

    return date;
}

From source file:iddb.web.viewbean.PenaltyViewBean.java

@Override
public String toString() {
    if (this.blank)
        return MessageResource.getMessage("banned");

    StringBuilder s = new StringBuilder();
    DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
    format.setTimeZone(TimeZone.getTimeZone("GMT-3"));

    s.append("Baneado el ").append(format.format(this.getCreated()));
    if (this.getDuration() > 0) {
        DateFormat format2 = new SimpleDateFormat("dd/MM/yyyy HH:mm");
        format2.setTimeZone(TimeZone.getTimeZone("GMT-3"));
        s.append(" hasta ").append(format2.format(this.getExpires()));
    }/*from ww w  .j  a v a 2  s  . c  o  m*/
    if (StringUtils.isNotEmpty(this.getReason())) {
        s.append(". Motivo: ").append(this.getReason());
    } else {
        s.append(". No indica motivo.");
    }
    if (StringUtils.isNotEmpty(this.getAdmin())) {
        s.append(" (").append(getAdmin()).append(")");
    }
    return s.toString();
}