Example usage for java.util TimeZone getTimeZone

List of usage examples for java.util TimeZone getTimeZone

Introduction

In this page you can find the example usage for java.util TimeZone getTimeZone.

Prototype

public static TimeZone getTimeZone(ZoneId zoneId) 

Source Link

Document

Gets the TimeZone for the given zoneId .

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    String fromDateString = "Wed Jul 08 17:08:48 GMT 2015";
    DateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
    Date fromDate = (Date) formatter.parse(fromDateString);
    TimeZone central = TimeZone.getTimeZone("America/Chicago");
    formatter.setTimeZone(central);//from  ww  w  . j  a v  a  2 s  .co  m
    System.out.println(formatter.format(fromDate));
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String input = "Thu Jun 06 2015 00:00:00 GMT+0530 (India Standard Time)";
    DateFormat inputFormat = new SimpleDateFormat("E MMM dd yyyy HH:mm:ss 'GMT'z", Locale.ENGLISH);
    Date date = inputFormat.parse(input);

    DateFormat outputFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss", Locale.ENGLISH);
    outputFormat.setTimeZone(TimeZone.getTimeZone("UTC"));

    String output = outputFormat.format(date);
    System.out.println(output);/* w  w  w. ja  v  a2 s.c om*/
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    JAXBContext jc = JAXBContext.newInstance(Root.class);

    Root root = new Root();
    root.defaultTimeZone = Calendar.getInstance();

    root.setTimeZone = Calendar.getInstance();
    root.setTimeZone.setTimeZone(TimeZone.getTimeZone("GMT"));

    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(root, System.out);
}

From source file:Main.java

public static void main(String[] args) {
    // Get the current date in the system default time zone
    GregorianCalendar currentDate = new GregorianCalendar();
    System.out.println(currentDate.getTime());
    // Get GregorianCalendar object representing March 21, 2014 07:30:45 AM
    GregorianCalendar someDate = new GregorianCalendar(2014, Calendar.MARCH, 21, 7, 30, 45);
    System.out.println(someDate.getTime());
    // Get Indian time zone, which is GMT+05:30
    TimeZone indianTZ = TimeZone.getTimeZone("GMT+05:30");
    GregorianCalendar indianDate = new GregorianCalendar(indianTZ);
    System.out.println(indianDate.getTime());
}

From source file:Main.java

public static void main(String[] args) {

    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:00Z");
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.YEAR, 2015);
    cal.set(Calendar.MONTH, Calendar.SEPTEMBER);
    cal.set(Calendar.DATE, 1);/*from   ww  w.j a  v a 2 s .c o  m*/
    cal.set(Calendar.HOUR_OF_DAY, 12);
    cal.set(Calendar.MINUTE, 15);
    Date date = cal.getTime();
    System.out.println(date);
    TimeZone tz = TimeZone.getTimeZone("IST");
    dateFormat.setTimeZone(tz);
    String actual = dateFormat.format(date);

    System.out.println(actual);
}

From source file:Main.java

public static void main(String[] args) {
    Calendar localTime = Calendar.getInstance();
    localTime.set(Calendar.HOUR, 17);
    localTime.set(Calendar.MINUTE, 15);
    localTime.set(Calendar.SECOND, 20);

    int hour = localTime.get(Calendar.HOUR);
    int minute = localTime.get(Calendar.MINUTE);
    int second = localTime.get(Calendar.SECOND);

    System.out.printf("Local time  : %02d:%02d:%02d\n", hour, minute, second);

    Calendar germanyTime = new GregorianCalendar(TimeZone.getTimeZone("Germany"));
    germanyTime.setTimeInMillis(localTime.getTimeInMillis());
    hour = germanyTime.get(Calendar.HOUR);
    minute = germanyTime.get(Calendar.MINUTE);
    second = germanyTime.get(Calendar.SECOND);

    System.out.printf("Germany time: %02d:%02d:%02d\n", hour, minute, second);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    Statement st = conn.createStatement();

    st.executeUpdate("create table survey (id int,myDate DATE );");
    String INSERT_RECORD = "insert into survey(id, myDate) values(?, ?)";

    PreparedStatement pstmt = conn.prepareStatement(INSERT_RECORD);
    pstmt.setString(1, "1");
    java.sql.Date sqlDate = new java.sql.Date(new java.util.Date().getTime());
    pstmt.setDate(2, sqlDate);/* w  ww . ja  v a2s .  co m*/

    pstmt.executeUpdate();

    ResultSet rs = st.executeQuery("SELECT * FROM survey");

    Calendar cal = Calendar.getInstance();

    // get the TimeZone for "America/Los_Angeles"
    TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");
    cal.setTimeZone(tz);

    while (rs.next()) {
        // the JDBC driver will use the time zone information in
        // Calendar to calculate the date, with the result that
        // the variable dateCreated contains a java.sql.Date object
        // that is accurate for "America/Los_Angeles".
        java.sql.Date dateCreated = rs.getDate(2, cal);
        System.out.println(dateCreated);
    }

    rs.close();
    st.close();
    conn.close();
}

From source file:com.venilnoronha.dzone.feed.DZoneFeedApplication.java

public static void main(String[] args) {
    TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
    SpringApplication.run(DZoneFeedApplication.class, args);
}

From source file:Main.java

public static void main(String[] args) {
    GregorianCalendar gc = new GregorianCalendar(2014, 1, 11, 15, 45, 50);
    LocalDate ld = gc.toZonedDateTime().toLocalDate();
    System.out.println("Local  Date: " + ld);

    LocalTime lt = gc.toZonedDateTime().toLocalTime();
    System.out.println("Local Time:  " + lt);

    LocalDateTime ldt = gc.toZonedDateTime().toLocalDateTime();
    System.out.println("Local DateTime:  " + ldt);

    OffsetDateTime od = gc.toZonedDateTime().toOffsetDateTime();
    System.out.println("Offset  Date: " + od);

    OffsetTime ot = gc.toZonedDateTime().toOffsetDateTime().toOffsetTime();
    System.out.println("Offset Time:  " + ot);

    ZonedDateTime zdt = gc.toZonedDateTime();
    System.out.println("Zoned DateTime:  " + zdt);

    ZoneId zoneId = zdt.getZone();
    TimeZone timeZone = TimeZone.getTimeZone(zoneId);
    System.out.println("Zone ID:  " + zoneId);
    System.out.println("Time Zone ID:  " + timeZone.getID());

    GregorianCalendar gc2 = GregorianCalendar.from(zdt);
    System.out.println("Gregorian  Calendar: " + gc2.getTime());
}

From source file:airnowgrib2tojson.AirNowGRIB2toJSON.java

/**
 * @param args the command line arguments
 *//*from  www. j ava 2 s.  c om*/
public static void main(String[] args) {

    SimpleDateFormat GMT = new SimpleDateFormat("yyMMddHH");
    GMT.setTimeZone(TimeZone.getTimeZone("GMT-2"));

    System.out.println(GMT.format(new Date()));

    FTPClient ftpClient = new FTPClient();
    FileOutputStream fos = null;

    try {
        //Connecting to AirNow FTP server to get the fresh AQI data  
        ftpClient.connect("ftp.airnowapi.org");
        ftpClient.login("pixelshade", "GZDN8uqduwvk");
        ftpClient.enterLocalPassiveMode();
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

        //downloading .grib2 file
        File of = new File("US-" + GMT.format(new Date()) + "_combined.grib2");
        OutputStream outstr = new BufferedOutputStream(new FileOutputStream(of));
        InputStream instr = ftpClient
                .retrieveFileStream("GRIB2/US-" + GMT.format(new Date()) + "_combined.grib2");
        byte[] bytesArray = new byte[4096];
        int bytesRead = -1;
        while ((bytesRead = instr.read(bytesArray)) != -1) {
            outstr.write(bytesArray, 0, bytesRead);
        }

        //Close used resources
        ftpClient.completePendingCommand();
        outstr.close();
        instr.close();

        // logout the user 
        ftpClient.logout();

    } catch (SocketException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            //disconnect from AirNow server
            ftpClient.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    try {
        //Open .grib2 file
        final File AQIfile = new File("US-" + GMT.format(new Date()) + "_combined.grib2");
        final GridDataset gridDS = GridDataset.open(AQIfile.getAbsolutePath());

        //The data type needed - AQI; since it isn't defined in GRIB2 standard,
        //Aerosol type is used instead; look AirNow API documentation for details.
        GridDatatype AQI = gridDS.findGridDatatype("Aerosol_type_msl");

        //Get the coordinate system for selected data type;
        //cut the rectangle to work with - time and height axes aren't present in these files
        //and latitude/longitude go "-1", which means all the data provided.
        GridCoordSystem AQIGCS = AQI.getCoordinateSystem();
        List<CoordinateAxis> AQI_XY = AQIGCS.getCoordinateAxes();
        Array AQIslice = AQI.readDataSlice(0, 0, -1, -1);

        //Variables for iterating through coordinates
        VariableDS var = AQI.getVariable();
        Index index = AQIslice.getIndex();

        //Variables for counting lat/long from the indices provided
        double stepX = (AQI_XY.get(2).getMaxValue() - AQI_XY.get(2).getMinValue()) / index.getShape(1);
        double stepY = (AQI_XY.get(1).getMaxValue() - AQI_XY.get(1).getMinValue()) / index.getShape(0);
        double curX = AQI_XY.get(2).getMinValue();
        double curY = AQI_XY.get(1).getMinValue();

        //Output details
        OutputStream ValLog = new FileOutputStream("USA_AQI.json");
        Writer ValWriter = new OutputStreamWriter(ValLog);

        for (int j = 0; j < index.getShape(0); j++) {
            for (int i = 0; i < index.getShape(1); i++) {
                float val = AQIslice.getFloat(index.set(j, i));

                //Write the AQI value and its coordinates if it's present by i/j indices
                if (!Float.isNaN(val))
                    ValWriter.write("{\r\n\"lat\":" + curX + ",\r\n\"lng\":" + curY + ",\r\n\"AQI\":" + val
                            + ",\r\n},\r\n");

                curX += stepX;
            }
            curY += stepY;
            curX = AQI_XY.get(2).getMinValue();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}