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:com.murrayc.galaxyzoo.app.QuestionFragment.java

private static String getCurrentDateTimeAsIso8601() {
    final Date now = new Date();
    //TODO: Is there a simpler way of getting an ISO-8601-formatted date,
    //or at least a way to avoid writing the format out manually here?
    final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);
    dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    return dateFormat.format(now);
}

From source file:com.healthmarketscience.jackcess.ExportTest.java

public void testExportToFile() throws Exception {
    DateFormat df = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
    df.setTimeZone(TEST_TZ);

    for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) {
        Database db = create(fileFormat);
        db.setTimeZone(TEST_TZ);/*from  w  ww  .  ja va 2 s . c om*/

        Table t = new TableBuilder("test").addColumn(new ColumnBuilder("col1", DataType.TEXT))
                .addColumn(new ColumnBuilder("col2", DataType.LONG))
                .addColumn(new ColumnBuilder("col3", DataType.DOUBLE))
                .addColumn(new ColumnBuilder("col4", DataType.OLE))
                .addColumn(new ColumnBuilder("col5", DataType.BOOLEAN))
                .addColumn(new ColumnBuilder("col6", DataType.SHORT_DATE_TIME)).toTable(db);

        Date testDate = df.parse("19801231 00:00:00");
        t.addRow("some text||some more", 13, 13.25, createString(30).getBytes(), true, testDate);

        t.addRow("crazy'data\"here", -345, -0.000345, createString(7).getBytes(), true, null);

        t.addRow("C:\\temp\\some_file.txt", 25, 0.0, null, false, null);

        StringWriter out = new StringWriter();

        new ExportUtil.Builder(db, "test").exportWriter(new BufferedWriter(out));

        String expected = "some text||some more,13,13.25,\"61 62 63 64  65 66 67 68  69 6A 6B 6C  6D 6E 6F 70  71 72 73 74  75 76 77 78"
                + NL + "79 7A 61 62  63 64\",true," + testDate + NL
                + "\"crazy'data\"\"here\",-345,-3.45E-4,61 62 63 64  65 66 67,true," + NL
                + "C:\\temp\\some_file.txt,25,0.0,,false," + NL;

        assertEquals(expected, out.toString());

        out = new StringWriter();

        new ExportUtil.Builder(db, "test").setHeader(true).setDelimiter("||").setQuote('\'')
                .exportWriter(new BufferedWriter(out));

        expected = "col1||col2||col3||col4||col5||col6" + NL
                + "'some text||some more'||13||13.25||'61 62 63 64  65 66 67 68  69 6A 6B 6C  6D 6E 6F 70  71 72 73 74  75 76 77 78"
                + NL + "79 7A 61 62  63 64'||true||" + testDate + NL
                + "'crazy''data\"here'||-345||-3.45E-4||61 62 63 64  65 66 67||true||" + NL
                + "C:\\temp\\some_file.txt||25||0.0||||false||" + NL;
        assertEquals(expected, out.toString());

        ExportFilter oddFilter = new SimpleExportFilter() {
            private int _num;

            @Override
            public Object[] filterRow(Object[] row) {
                if ((_num++ % 2) == 1) {
                    return null;
                }
                return row;
            }
        };

        out = new StringWriter();

        new ExportUtil.Builder(db, "test").setFilter(oddFilter).exportWriter(new BufferedWriter(out));

        expected = "some text||some more,13,13.25,\"61 62 63 64  65 66 67 68  69 6A 6B 6C  6D 6E 6F 70  71 72 73 74  75 76 77 78"
                + NL + "79 7A 61 62  63 64\",true," + testDate + NL + "C:\\temp\\some_file.txt,25,0.0,,false,"
                + NL;

        assertEquals(expected, out.toString());
    }
}

From source file:crawler.AScraper.java

@Transformer(inputChannel = "channel3", outputChannel = "channel4")
public Artwork convert(Element payload) throws ParseException, MalformedURLException {
    Matcher m = patter.matcher(payload.text());
    if (m.find()) {
        String year = m.group("year");
        String month = m.group("month");
        String day = m.group("day");
        int id = Integer.parseInt(m.group("id"));
        String model = m.group("model").split("[\\s\\[\\]]")[0];
        URL link = new URL(payload.attr("href"));
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        format.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        Date date = format.parse(String.format("%s-%s-%s", year, month, day));
        String thread_title = payload.text();
        return new Artwork(thread_title, id, -1, -1, null, link, null, model, date);
    } else {/*  w w  w  .  ja  va2  s .c o m*/
        LOG.error(payload.text());
        return null;
    }

}

From source file:org.jivesoftware.util.JiveGlobals.java

/**
 * Formats a Date object to return a time using the global locale.
 *
 * @param date the Date to format.//w ww  .  jav a 2 s  .  co m
 * @return a String representing the time.
 */
public static String formatTime(Date date) {
    if (timeFormat == null) {
        if (properties != null) {
            timeFormat = DateFormat.getTimeInstance(DateFormat.SHORT, getLocale());
            timeFormat.setTimeZone(getTimeZone());
        } else {
            DateFormat instance = DateFormat.getTimeInstance(DateFormat.SHORT, getLocale());
            instance.setTimeZone(getTimeZone());
            return instance.format(date);
        }
    }
    return timeFormat.format(date);
}

From source file:org.jivesoftware.util.JiveGlobals.java

/**
 * Formats a Date object to return a date using the global locale.
 *
 * @param date the Date to format.//  www.  jav  a2 s. c  o m
 * @return a String representing the date.
 */
public static String formatDate(Date date) {
    if (dateFormat == null) {
        if (properties != null) {
            dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, getLocale());
            dateFormat.setTimeZone(getTimeZone());
        } else {
            DateFormat instance = DateFormat.getDateInstance(DateFormat.MEDIUM, getLocale());
            instance.setTimeZone(getTimeZone());
            return instance.format(date);
        }
    }
    return dateFormat.format(date);
}

From source file:org.carcv.web.servlet.CarTableServlet.java

/**
 * @see CarTableServlet/* w w w.j  a  va2s . co  m*/
 * @param request the HttpServletRequest
 * @param response the HttpServletResponse
 * @throws ServletException
 * @throws IOException
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    final ArrayList<FileEntry> requestEntries = (ArrayList<FileEntry>) bean.getAll();

    @SuppressWarnings("unchecked")
    ArrayList<FileEntry> abstractEntries = (ArrayList<FileEntry>) requestEntries.clone();

    // Timezone
    String timeZoneStr = request.getParameter("timezone");
    TimeZone tz = TimeZone.getTimeZone(timeZoneStr);

    DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
    dateFormat.setTimeZone(tz);
    DateFormat timeFormat = new SimpleDateFormat("HH:mm");
    timeFormat.setTimeZone(tz);

    Collections.sort(abstractEntries, new Comparator<FileEntry>() {
        @Override
        public int compare(FileEntry o1, FileEntry o2) {
            return new CompareToBuilder().append(o2.getCarData().getTimestamp(), o1.getCarData().getTimestamp())
                    .toComparison();
        }
    });

    String date, time, entryId, licensePlate, location, previewPath;
    ArrayList<WebReportTableMember> wrtmList = new ArrayList<>();

    for (FileEntry e : abstractEntries) {
        // initialize
        date = dateFormat.format(e.getCarData().getTimestamp());
        time = timeFormat.format(e.getCarData().getTimestamp());

        entryId = e.getId().toString();

        licensePlate = e.getCarData().getNumberPlate().getOrigin() + ": "
                + e.getCarData().getNumberPlate().getText();

        location = e.getCarData().getAddress().printBR();

        FileCarImage fci = e.getCarImages().get(0);
        previewPath = fci.getFilepath().toString();

        WebReportTableMember wrtm = new WebReportTableMember(previewPath, entryId, time, date, location,
                licensePlate, timeZoneStr);

        wrtmList.add(wrtm);
    }

    request.setAttribute("wrtmList", wrtmList);

    boolean isAdmin = request.isUserInRole(adminRole);
    request.setAttribute("isAdmin", isAdmin);

    RequestDispatcher rd = getServletContext().getRequestDispatcher("/app/cartable.jsp");
    rd.forward(request, response);
}

From source file:com.redhat.lightblue.metadata.types.DateTypeTest.java

@Test
public void testToJson() {
    DateFormat dateFormat = new SimpleDateFormat(DateType.DATE_FORMAT_STR);
    dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));

    String date = dateFormat.format(new Date());
    JsonNodeFactory jsonNodeFactory = new JsonNodeFactory(true);
    JsonNode jsonNode = dateType.toJson(jsonNodeFactory, date);
    assertTrue(jsonNode.asText().equals(date));
}

From source file:com.persistent.cloudninja.controller.RunningInstancesJSONDataController.java

public List<String> generateStartAndEndTime(int year, int month) {
    DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss.sss");
    dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));

    Calendar cal = Calendar.getInstance();
    cal.setTimeZone(TimeZone.getTimeZone("UTC"));

    Date currentDate = cal.getTime();
    int currentMonth = cal.get(Calendar.MONTH) + 1;

    String start = month + "/01/" + year + " 00:00:00.000";
    String end = null;/*from w  ww.  j  a  v a2  s .c  om*/

    if (month == currentMonth) {
        end = dateFormat.format(currentDate);
    } else {
        try {
            cal.setTime(dateFormat.parse(start));
            int maxDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
            end = month + "/" + maxDay + "/" + year + " 23:59:59.000";

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

    }
    List<String> time = new ArrayList<String>();
    time.add(start);
    time.add(end);
    return time;
}

From source file:ch.bfh.uniboard.accesscontrolled.AccessControlledServiceProtectedTest.java

@Test
public void testCreateMessageElement() {

    AccessControlledService service = new AccessControlledService();
    byte[] message = new byte[1];
    message[0] = 0x6;/*from   ww w.  j  a va 2s .  c o m*/
    String string = "test";
    int integer = 10;
    byte[] byteA = new byte[1];
    byteA[0] = 0x7;
    Date date = new Date();
    Attributes alpha = new Attributes();
    alpha.add("a1", new StringValue(string));
    alpha.add("a2", new IntegerValue(integer));
    alpha.add("a3", new ByteArrayValue(byteA));
    alpha.add("a4", new DateValue(date));
    Element result = service.createMessageElement(message, alpha);
    assertTrue(result instanceof Pair);
    Pair resultPair = (Pair) result;
    ByteArrayElement resultMessage = (ByteArrayElement) resultPair.getFirst();
    assertArrayEquals(resultMessage.getValue().getBytes(), message);

    assertTrue(resultPair.getSecond().isTuple());
    Tuple alphaResult = (Tuple) resultPair.getSecond();
    assertEquals(string, ((StringElement) alphaResult.getAt(0)).getValue());
    assertEquals(integer, ((ZElement) alphaResult.getAt(1)).getValue().intValue());
    assertArrayEquals(byteA, ((ByteArrayElement) alphaResult.getAt(2)).getValue().getBytes());
    TimeZone timeZone = TimeZone.getTimeZone("UTC");
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
    dateFormat.setTimeZone(timeZone);
    String expectedDateString = dateFormat.format(date);
    assertEquals(expectedDateString, ((StringElement) alphaResult.getAt(3)).getValue());

}

From source file:com.haulmont.chile.core.datatypes.impl.DateTimeDatatype.java

@Override
public String format(@Nullable Object value, Locale locale, TimeZone timeZone) {
    if (value == null) {
        return "";
    }/*from w w w .  jav  a  2s. c o  m*/

    FormatStrings formatStrings = AppBeans.get(FormatStringsRegistry.class).getFormatStrings(locale);
    if (formatStrings == null) {
        return format(value);
    }

    DateFormat format = new SimpleDateFormat(formatStrings.getDateTimeFormat());
    if (timeZone != null) {
        format.setTimeZone(timeZone);
    }

    return format.format(value);
}