Java Date ISO Parse ISOToJulianDate(String dateObs)

Here you can find the source of ISOToJulianDate(String dateObs)

Description

Transforms an ISO date to a julian date.

License

Open Source License

Parameter

Parameter Description
dateObs observation date as ISO format

Exception

Parameter Description
ParseException When the dateObs format is wrong

Return

a Julian date

Declaration

public static double ISOToJulianDate(String dateObs) throws ParseException 

Method Source Code


//package com.java2s;
/* /*from ww w .  j  a va 2 s  . c om*/
 * Copyright (C) 2014 Jean-Christophe Malapert
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

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

public class Main {
    /**
     * Transforms an ISO date to a julian date.
     *
     * @param dateObs observation date as ISO format
     * @return a Julian date
     * @throws ParseException When the dateObs format is wrong
     */
    public static double ISOToJulianDate(String dateObs) throws ParseException {
        SimpleDateFormat sdf;
        if (dateObs.contains("T") && dateObs.contains(".")) {
            sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.sss");
        } else if (dateObs.contains("T")) {
            sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        } else if (dateObs.contains("-")) {
            sdf = new SimpleDateFormat("yyyy-MM-dd");
        } else {
            sdf = new SimpleDateFormat("dd/MM/yy");
        }

        Date date = sdf.parse(dateObs);

        GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTime(date);

        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        int second = calendar.get(Calendar.SECOND);

        double extra = (100.0 * year) + month - 190002.5;
        return (367.0 * year) - (Math.floor(7.0 * (year + Math.floor((month + 9.0) / 12.0)) / 4.0))
                + Math.floor((275.0 * month) / 9.0) + day + ((hour + ((minute + (second / 60.0)) / 60.0)) / 24.0)
                + 1721013.5 - ((0.5 * extra) / Math.abs(extra)) + 0.5;
    }
}

Related

  1. iso8601ToCalendar(String s)
  2. iso8601ToDate(String s)
  3. ISO8601ToSeconds(String iso8601)
  4. isoDateStringToDate(String dateString)
  5. isOneOf(String value, String... values)
  6. isValidISO8601(String time)
  7. normalizeToISO8601(String sDate, TimeZone tz)
  8. parseAwsFlavouredISO8601Date(String dateString)
  9. parseDate(String iso8061StrDateTime)