java 如何在java中将度分秒转换为十进制

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2779405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 22:48:00  来源:igfitidea点击:

How to convert degree minutes second into decimal in java

java

提问by Satya

This is a basically gps application where i am getting the latitude information from the meta data of a picture in this format 28"41'44.13597 .

这是一个基本的 GPS 应用程序,我从这种格式的图片元数据中获取纬度信息 28"41'44.13597 。

My need is to convert the same information into decimal and the out will show data in decimal format like 28.705450.

我需要将相同的信息转换为十进制,输出将以十进制格式显示数据,如 28.705450。

Please help through code or any references

请帮助通过代码或任何参考

Thanks in advance

提前致谢

采纳答案by TiansHUo

/** answer=hour+minutes/60+seconds/3600 */
public double convertHourToDecimal(String degree) { 
    if(!degree.matches("(-)?[0-6][0-9]\"[0-6][0-9]\'[0-6][0-9](.[0-9]{1,5})?")
        throw new IllegalArgumentException();
    String[] strArray=degree.split("[\"']");
    return Double.parseDouble(strArray[0])+Double.parseDouble(strArray[1])/60+Double.parseDouble(strArray[2])/3600;
}

回答by Ignacio Vazquez-Abrams

Divide the minutes by 60.and the seconds by 3600., then add the three together.

将分钟除以60.,将秒除以3600.,然后将三者相加。

回答by Dimitar

I dont know of any java library that will do this for you but the formula to convert from degrees to decimal degrees is: degree + (minutes / 60) + (seconds / (60 * 60))

我不知道有任何 Java 库可以为您执行此操作,但是从度数转换为十进制度数的公式是:degree + (minutes / 60) + (seconds / (60 * 60))

回答by Satya

package newstract;

import java.io.File;
import java.util.Date;
import com.drew.imaging.jpeg.JpegMetadataReader;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.exif.ExifDirectory;
import java.text.SimpleDateFormat;
import com.drew.metadata.exif.GpsDirectory;

public class GetTagInfo {
    public static void main(String[] args) 
    {
        System.out.println("Picture Tagged Details");
        try{
        File jpegFile = new File("DSC_0060.JPG"); 
        Metadata metadata = JpegMetadataReader.readMetadata(jpegFile);
        Directory exifDirectory = metadata.getDirectory(ExifDirectory.class);
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss");
        Date myDate = exifDirectory.getDate(ExifDirectory.TAG_DATETIME); 
        System.out.println(sdf.format(myDate));  
        SimpleDateFormat sdf1 = new SimpleDateFormat("MM/dd/yyyy");
        Date myDate1 = exifDirectory.getDate(ExifDirectory.TAG_DATETIME); 
        System.out.println(sdf1.format(myDate1));  
        SimpleDateFormat sdf2 = new SimpleDateFormat("hh:mm:ss");
        Date myDate3 = exifDirectory.getDate(ExifDirectory.TAG_DATETIME); 
        System.out.println(sdf2.format(myDate3));  


        Directory gpsDirectory = metadata.getDirectory(GpsDirectory.class);
       // Boolean b = (gpsDirectory.containsTag(GpsDirectory.TAG_GPS_LATITUDE));
       // System.out.println(GpsDirectory.TAG_GPS_LATITUDE);+
        String s = gpsDirectory.getDescription(2);
        System.out.println(s);
        SplitString1 w = new SplitString1();
        w.doit(s);



        Iterator directories = metadata.getDirectoryIterator();
        while (directories.hasNext()) {
        GpsDescriptor directory = (GpsDescriptor) directories.next();
        System.out.print(directory.getGpsLatitudeDescription());
        }

        } // close of catch
        catch (Exception e) {
            System.err.println(e.getMessage());
            //System.err.println(tag.getDirectoryName() + " " + tag.getTagName() + " (error)");
        }

}

}

 class SplitString1 {

    public void doit(String lat) {

        String str = lat;
        String [] temp = null;
        String dtemp = null;
        //temp = str.split("[\"]|\"[\']");
        temp = str.split("[\"]|[\']" ); 
        dtemp = str.replace("\"", "°");
        System.out.println("Formated DCM : "+dtemp);
        dump(temp);


    }

    public void dump(String []s) {
        for (int i = 0 ; i < s.length ; i++) {
            System.out.println("\ndegree : "+s[0]);
            System.out.println("\nminutes : "+s[1]);
            System.out.println("\nsecond : "+s[2]);

            String deg = s[0] ;
            int ndeg = Integer.parseInt(deg);
            String min = s[1] ;
            double nmin = Double.parseDouble(min);
            String sec = s[2] ;
            double nsec = Double.parseDouble(sec);
            double decimaldms = (ndeg+(nmin/60)+(nsec/3600));
            System.out.println("\nfinaldecimal : "+decimaldms);
        }
    }

    // Decimal degrees = whole number of degrees, plus minutes divided by 60, 
    //plus seconds divided by 3600
}