Java Date Format convertDateToString(Date date)

Here you can find the source of convertDateToString(Date date)

Description

convert Date To String

License

Apache License

Declaration

public static String convertDateToString(Date date) 

Method Source Code

//package com.java2s;
/*/* www  . j a v a 2 s.c o  m*/
 * jtoggl - Java Wrapper for Toggl REST API https://www.toggl.com/public/api
 *
 * Copyright (C) 2011 by simas GmbH, Moosentli 7, 3235 Erlach, Switzerland
 * http://www.simas.ch
 * 
 * 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.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Main {
    public static final String DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss";

    public static String convertDateToString(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int timezoneOffset = -(cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET)) / (60 * 1000);
        int hour = Math.abs(timezoneOffset / 60);
        int min = Math.abs(timezoneOffset % 60);
        String dateTime = sdf.format(date);
        String timeOffset = (timezoneOffset <= 0 ? "+" : "-") + (hour < 10 ? "0" : "") + hour + ":"
                + (min < 10 ? "0" : "") + min;
        return dateTime + timeOffset;
    }
}

Related

  1. convertDateToMMDDYYYY(Date date)
  2. convertDateToRpcFormat(Date date)
  3. convertDateToString(Date aDate)
  4. convertDateToString(Date aDate)
  5. convertDateToString(Date date)
  6. convertDateToString(Date date)
  7. convertDateToString(Date date)
  8. convertDateToString(Date date)
  9. convertDateToString(Date date)