Java Calendar to String calendarToString(Calendar date, boolean transformToLocalTime)

Here you can find the source of calendarToString(Calendar date, boolean transformToLocalTime)

Description

calendar To String

License

Apache License

Declaration

public static String calendarToString(Calendar date, boolean transformToLocalTime) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

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

import java.util.TimeZone;

public class Main {
    public static String calendarToString(Calendar date, boolean transformToLocalTime) {
        // clienttan alacagimiz degerlerle dolduracagiz
        int clientZoneOffset = 7200000; // milisecond
        int clientDSTOffset = 3600000; // milisecond

        String stringDate = "";

        if (!transformToLocalTime) {

            stringDate = appendZero(date.get(Calendar.DATE)) + "." + appendZero(date.get(Calendar.MONTH) + 1) + "."
                    + date.get(Calendar.YEAR);
            stringDate += " " + appendZero(date.get(Calendar.HOUR_OF_DAY)) + ":"
                    + appendZero(date.get(Calendar.MINUTE)) + ":" + appendZero(date.get(Calendar.SECOND));

            int localTimeShift = clientDSTOffset + clientZoneOffset;
            int dateTimeShift = date.get(Calendar.DST_OFFSET) + date.get(Calendar.ZONE_OFFSET);

            if (localTimeShift != dateTimeShift) {
                stringDate += getTimeZoneStr(dateTimeShift);
            }//www  . j  a  v  a2  s  . co m

        } else {
            SimpleDateFormat formatter = null;
            formatter = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");

            TimeZone timeZone = TimeZone.getDefault();
            if (clientDSTOffset == timeZone.getDSTSavings()) {
                timeZone.setRawOffset(clientZoneOffset);
            } else if (clientDSTOffset > timeZone.getDSTSavings()) {
                timeZone.setRawOffset(clientZoneOffset + clientDSTOffset);
            } else {
                timeZone.setRawOffset(clientZoneOffset - timeZone.getDSTSavings());
            }
            formatter.setTimeZone(timeZone);

            stringDate = formatter.format(date.getTime());
        }

        return stringDate;
    }

    public static String appendZero(int value) {
        String zeroAppendedValue = "" + value;

        if (value < 10 && (value + "").length() == 1) {
            zeroAppendedValue = "0" + value;
        }

        return zeroAppendedValue;
    }

    private static String getTimeZoneStr(int timeShift) {

        String timeZome = "";

        if (timeShift > 0) {
            timeZome += "+";

        } else {
            timeZome += "-";

            timeShift = timeShift * (-1);
        }

        int zoneHour = timeShift / 3600000;
        int zoneMin = (timeShift - zoneHour * 3600000) / 60000;

        timeZome += appendZero(zoneHour) + ":" + appendZero(zoneMin);

        return timeZome;
    }

    public static String getTime(Date executionTime) {
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
        return formatter.format(executionTime);
    }

    public static Date getTime(String timeInString) {

        DateFormat dateFormat = new SimpleDateFormat("hh:mm:ss");
        Date date = null;

        try {

            date = dateFormat.parse(timeInString);

            // System.out.println("Date and Time: " + date);

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

        return date;

    }
}

Related

  1. calendarToString(Calendar cal, String format)
  2. calendarToString(Calendar calendar)
  3. calendarToString(Calendar calendar)
  4. calendarToString(Calendar calendar)
  5. calendarToString(Calendar calendar, String template)
  6. CalendarToString(Calendar fecha, String formato)
  7. calendarToXSDString(Calendar cal, String fmt)
  8. dateToCalendar(final String date)
  9. dateToString(Calendar date, DateFormat format)