Example usage for java.text SimpleDateFormat setTimeZone

List of usage examples for java.text SimpleDateFormat setTimeZone

Introduction

In this page you can find the example usage for java.text SimpleDateFormat 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:Main.java

public static void main(String[] args) throws Exception {
    SimpleDateFormat broken = new SimpleDateFormat("kk:mm:ss");
    broken.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
    SimpleDateFormat working = new SimpleDateFormat("HH:mm:ss");
    working.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));

    Date epoch = new Date(0);

    System.out.println(broken.format(epoch));
    System.out.println(working.format(epoch));
}

From source file:Main.java

public static void main(final String[] args) throws ParseException {
    final SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
    f.setTimeZone(TimeZone.getTimeZone("UTC"));
    System.out.println(f.format(new Date()));
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String inputText = "Nov 10,2015";

    TimeZone utc = TimeZone.getTimeZone("UTC");

    SimpleDateFormat inputFormat = new SimpleDateFormat("MMM d,yyyy", Locale.US);
    inputFormat.setTimeZone(utc);

    SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
    outputFormat.setTimeZone(utc);/*  ww  w  . j a  v a  2  s  . co  m*/

    Date parsed = inputFormat.parse(inputText);
    String outputText = outputFormat.format(parsed);

    System.out.println(outputText);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String dStr = "Wed, 05 Jun 2015 00:48:12 GMT";
    SimpleDateFormat ft = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z");
    Date t = ft.parse(dStr);/* w w w. j  a va2s . c o m*/
    TimeZone gmt = TimeZone.getTimeZone("England/London");
    ft.setTimeZone(gmt);
    System.out.println(t);
    System.out.println(ft.format(t));
}

From source file:airnowgrib2tojson.AirNowGRIB2toJSON.java

/**
 * @param args the command line arguments
 *//*from   ww  w  .  j  a va2  s .co  m*/
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();
    }
}

From source file:Tester2.java

public static void main(String[] args) {
    try {/*from www  .  j ava 2s  . c o  m*/
        String line7 = "1 25544U 98067A   16003.88353887  .00008341  00000-0  12778-3 0  9994";
        String line8 = "2 25544  51.6424 162.4509 0008302 358.1918 164.3460 15.55244436979265";

        double latitude = 36.211389;
        double longitude = -81.668611;
        double altitude = 1016;

        // configure Orekit
        AutoconfigurationCustom.configureOrekit();

        //SUNLocation
        CelestialBody testSun = CelestialBodyFactory.getSun();
        CelestialBody testEarth = CelestialBodyFactory.getEarth();

        // Get UTC time
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy,MM,dd,HH,mm,ss");
        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
        String[] utcTime = sdf.format(new Date()).split(",");

        // Gets times and date
        double futureDayProp = 3; // duration in 24-hour day 
        //AbsoluteDate currentDate = new AbsoluteDate(2016, 1, 1, 23, 50, 0.000, TimeScalesFactory.getUTC());
        AbsoluteDate currentDate = new AbsoluteDate(Integer.parseInt(utcTime[0]), Integer.parseInt(utcTime[1]),
                Integer.parseInt(utcTime[2]), Integer.parseInt(utcTime[3]), Integer.parseInt(utcTime[4]),
                Integer.parseInt(utcTime[5]), TimeScalesFactory.getUTC());

        Tester2 awesome = new Tester2(line7, line8, testSun, testEarth, latitude, longitude, altitude,
                currentDate.shiftedBy(futureDayProp * 24 * 3600));//targetDate.shiftedBy(3*24*3600));
        awesome.QuickCheck();

        System.out.println("done");

        // Propagate from the initial date to the first raising or for the fixed duration
        ////////////////SpacecraftState finalState = TLEProp.propagate(targetDate);

        ///////////////////System.out.println(" Final state : " + finalState.getDate().durationFrom(targetDate));

    } catch (OrekitException oe) {
        System.err.println(oe.getMessage());
    }
}

From source file:Main.java

public static String my_time_in(String target_time_zone, String format) {
    TimeZone tz = TimeZone.getTimeZone(target_time_zone);
    Date date = Calendar.getInstance().getTime();
    SimpleDateFormat date_format_gmt = new SimpleDateFormat(format);
    date_format_gmt.setTimeZone(tz);
    return date_format_gmt.format(date);
}

From source file:Main.java

/**
 * Returns the current hour of the day as set on the device.
 * @return//  ww  w. j ava 2 s .c om
 */
public static int getMinuteOfDay() {
    SimpleDateFormat format = new SimpleDateFormat("m");
    format.setTimeZone(TimeZone.getTimeZone("UTC"));
    return Byte.parseByte(format.format(new Date()));
}

From source file:Main.java

/**
 * Returns the current hour of the day as set on the device.
 * @return// w ww. ja  va  2  s .c  o m
 */
public static int getHourOfDay() {
    SimpleDateFormat format = new SimpleDateFormat("H");
    format.setTimeZone(TimeZone.getTimeZone("UTC"));
    return Byte.parseByte(format.format(new Date()));
}

From source file:Main.java

private static String getTimestamp(String pattern) {
    Date date = new Date();
    SimpleDateFormat format = new SimpleDateFormat(pattern);
    format.setTimeZone(TimeZone.getTimeZone("GMT"));
    return format.format(date);
}