Beidou time week and GPS time week calculation, JAVA as an example

    Recently, I received a request to analyze the files downloaded from the official website of NASA in the United States and the files downloaded from the official website of Beidou. The names of the files are named after the GPS time week and the day of the week, and the Beidou time week and the day of the week. There will be new data every day, and scheduled tasks Obtaining files and timing tasks requires the program to automatically calculate the file name according to the time of the day.

   If you want to calculate these two time weeks, you must first understand their respective calculation rules

  The GPS time starts from "1980-01-06 00:00:00",

   Beidou time starts from "2006-01-01 00:00:00"

  These two starting times are the most critical part of solving the problem

  To calculate the time week, you need to get the current time, subtract the respective start time, get the number of days, and then calculate the week and day of the week

/**
 * 计算两个时间之间的天数
 * @author yxt
 * @param d1
 * @param d2
 * @return
 */
public static long calendarminus(Calendar d1, Calendar d2) {
    if (d1 == null || d2 == null) {
        return 0;

    }
    return (d1.getTimeInMillis() - d2.getTimeInMillis()) / (3600 * 24000);

}

/**
 * 获取gps时间周和北斗时间周
 * * @author yxt
 * @throws Exception
 */
public static void getGpsAndBeidouWeek() throws Exception{

    //2021.5.11的文件名为:GPS_DATA_21571.text、BEIDOU_DATA_8011.text
    //其中21571中前四位为gps时间周,最后一位为周几(以周一为0)
    //其中8011中前三位为北斗时间周,最后一位为周几(以周一为0)
    //计算的关键在于知道gps开始时间为"1980-01-06 00:00:00",北斗开始时间为:"2006-01-01 00:00:00"
    SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//日期格式化
    Calendar calendarNow = Calendar.getInstance(); //

    // gps时间周计算
    Calendar calendarGps= Calendar.getInstance(); //java.util包
    Date gpsStartDate = sdfTime.parse("1980-01-06 00:00:00");
    calendarGps.setTime(gpsStartDate);
    long gpsToTodayDays = calendarminus(calendarNow, calendarGps);
    long gpsWeeks = gpsToTodayDays / 7;
    long dayOfGpsWeek = gpsToTodayDays % 7-1;
    System.out.println(gpsWeeks);
    System.out.println(dayOfGpsWeek);

    // 北斗时间周计算
    Calendar calendarBeidou= Calendar.getInstance(); //java.util包
    Date beidouStartDate = sdfTime.parse("2006-01-01 00:00:00");
    calendarBeidou.setTime(beidouStartDate);
    long beidouToTodayDays = calendarminus(calendarNow, calendarBeidou);
    long beidouWeeks = beidouToTodayDays / 7;
    long dayOfBeidouWeek = beidouToTodayDays % 7 -1;
    System.out.println(beidouWeeks);
    System.out.println(dayOfBeidouWeek);

}

 

Guess you like

Origin blog.csdn.net/weixin_41267342/article/details/116677087