Java Unix Date stringUnixTime(String start, String end)

Here you can find the source of stringUnixTime(String start, String end)

Description

string Unix Time

License

Apache License

Declaration

public static Map<String, Long> stringUnixTime(String start, String end) 

Method Source Code

//package com.java2s;
/*//w  w w.j a v  a  2s.c  om
 * Copyright 2016 tokwii.
 *
 * 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.util.Map;

import java.util.HashMap;

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

public class Main {
    public static Map<String, Long> stringUnixTime(String start, String end) {
        try {
            HashMap<String, Long> strUnixTime = new HashMap<String, Long>();
            String startTime = start.split("T")[1];
            String endTime = end.split("T")[1];
            int startIndex = timeToIndex(startTime);
            int endIndex = timeToIndex(endTime);
            SimpleDateFormat dateFormat = new SimpleDateFormat(
                    "yyyy-mm-dd HH:mm");
            dateFormat
                    .setTimeZone(TimeZone.getTimeZone("America/New_York"));
            for (int hour = startIndex; hour <= endIndex; hour++) {
                String dateString = start.split("T")[0] + " "
                        + indexToTime(hour);
                Date date = dateFormat.parse(dateString);
                long unixTime = date.getTime() / 1000;
                String timeStamp = start.split("T")[0] + "T"
                        + indexToTime(hour);
                strUnixTime.put(timeStamp, Long.valueOf(unixTime));
            }
            return strUnixTime;
        } catch (ParseException e) {
            System.out.println(e.getStackTrace());
            return null;
        }
    }

    public static int timeToIndex(String twentyFourHourTime) {
        String[] timeArray = twentyFourHourTime.split(":");
        if (Integer.parseInt(timeArray[1]) == 30) {
            return 2 * Integer.parseInt(timeArray[0]) + 1;
        } else {
            return 2 * Integer.parseInt(timeArray[0]);
        }
    }

    public static String indexToTime(int index) {
        if (index % 2 == 0) {
            String hour = Integer.toString(index / 2);
            String time = hour + ":00";
            if (time.length() != 5) {
                time = "0" + time;
            }
            return time;
        } else {
            String hour = Integer.toString(index / 2);
            String time = hour + ":30";
            if (time.length() != 5) {
                time = "0" + time;
            }
            return time;
        }

    }
}

Related

  1. getUnixDate(long millis)
  2. getUnixDate(String s)
  3. getUnixTime(String dateTime)
  4. getUnixTime2NoneTime(String longTime)
  5. parseUnixDate(String paramName, String dateStr)
  6. toUnixTime(Date date)
  7. toUnixTime(SimpleDateFormat simpleDateFormat, String audioboxDate)
  8. unixTime()
  9. unixTimeToDate(long unixTime)