Java Parse Date parseDateTimeString(String dateTime)

Here you can find the source of parseDateTimeString(String dateTime)

Description

Parse time stamp from elfcloud.fi server

License

Apache License

Parameter

Parameter Description
dateTime time stamp returned from elfcloud.fi server

Return

time stamp represented in local time

Declaration

public static String parseDateTimeString(String dateTime) 

Method Source Code

//package com.java2s;
/*//  w w w  .j  a v a 2s .  c  o m
 * Copyright 2010-2012 elfCLOUD / elfcloud.fi -?? SCIS Secure Cloud Infrastructure Services
 *   
 *      Licensed under the Apache License, Version 2.0 (the "License");
 *      you may not use this file except in compliance with the License.
 *      You may obtain a copy of the License at
 *   
 *         http://www.apache.org/licenses/LICENSE-2.0
 *   
 *         Unless required by applicable law or agreed to in writing, software
 *         distributed under the License is distributed on an "AS IS" BASIS,
 *         WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *         See the License for the specific language governing permissions and
 *         limitations under the License.
 */

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import java.util.TimeZone;

public class Main {
    /**
     * Parse time stamp from elfcloud.fi server 
     * @param dateTime time stamp returned from elfcloud.fi server
     * @return time stamp represented in local time
     */
    public static String parseDateTimeString(String dateTime) {
        if (dateTime == null || dateTime.length() == 0) {
            return "";
        }
        Calendar c = Calendar.getInstance();
        TimeZone z = c.getTimeZone();
        // 2012-09-26T06:58:43.446859+00:00
        SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        inputFormat.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));

        SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm Z");
        outputFormat.setTimeZone(z);
        String[] parts = dateTime.split("[T.]");
        try {
            Date date = inputFormat.parse(parts[0] + " " + parts[1]);
            String outputText = outputFormat.format(date);
            return outputText;
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return "";
    }
}

Related

  1. parseDateTime(String s)
  2. parseDatetime(String value)
  3. parseDateTimeFromString(String datetime)
  4. parseDateTimeHuman(String dateString)
  5. parseDateTimeString(final String date)
  6. parseDateTimeString(String dateTimeString)
  7. parseDateToStr(Date date)
  8. parseDateToString(Date date, String format)
  9. parseDateToString(Date date, String pattern)