格式化从ExifInterface.TAG_DATETIME获取的时间和日期

时间:2015-02-13 14:52:02

标签: android date datetime time

我正在开发一款应用。我得到时间,日期,经度和纬度。

所有信息都成功。

现在我想格式化ExifInterface.TAG_DATETIME的日期和时间。

我该怎么做?

MainActivity.java

public class DisplayImage extends Activity {
    ImageView displayImage;
    VideoView displayVideo;
    TextView imgInfo;
    DatabaseHelper dbHelper;
    String mediaPath = null;

    private boolean valid = false;
    ExifInterface exifInterface;

    LocationManager locationManager;
    Location location;
    Geocoder geocoder;
    List<Address> addresses;
    Double Latitude, Longitude;
    String zip, city, state, country;
    String imgDate_Time, attrLATITUDE, attrLATITUDE_REF, attrLONGITUDE, attrLONGITUDE_REF;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.display_image);

        displayImage = (ImageView) findViewById(R.id.displayImage);
        displayVideo = (VideoView) findViewById(R.id.displayVideo);
        imgInfo = (TextView) findViewById(R.id.txtImgInfo);

        dbHelper = new DatabaseHelper(getApplicationContext());
        mediaPath = dbHelper.displayImages();
        File mediaFile = new File(mediaPath);

        String title = "";
        geocoder = new Geocoder(DisplayImage.this, Locale.getDefault());

        try {
            exifInterface = new ExifInterface(mediaPath);

            imgDate_Time = exifInterface.getAttribute(ExifInterface.TAG_DATETIME);

            attrLATITUDE = exifInterface.getAttribute(ExifInterface.TAG_GPS_LATITUDE);
            attrLATITUDE_REF = exifInterface.getAttribute(ExifInterface.TAG_GPS_LATITUDE_REF);
            attrLONGITUDE = exifInterface.getAttribute(ExifInterface.TAG_GPS_LONGITUDE);
            attrLONGITUDE_REF = exifInterface.getAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF);

            if ((attrLATITUDE != null) && (attrLATITUDE_REF != null) && (attrLONGITUDE != null)
                    && (attrLONGITUDE_REF != null)) {
                valid = true;

                if (attrLATITUDE_REF.equals("N")) {
                    Latitude = convertToDegree(attrLATITUDE);
                } else {
                    Latitude = 0 - convertToDegree(attrLATITUDE);
                }

                if (attrLONGITUDE_REF.equals("E")) {
                    Longitude = convertToDegree(attrLONGITUDE);
                } else {
                    Longitude = 0 - convertToDegree(attrLONGITUDE);
                }

                addresses = geocoder.getFromLocation(Latitude, Longitude, 1);
                if (addresses != null && addresses.size() > 0) {

                    zip = addresses.get(0).getPostalCode();
                    city = addresses.get(0).getLocality();
                    state = addresses.get(0).getAdminArea();
                    country = addresses.get(0).getCountryName();
                    if (zip != null) {
                        title += zip + ",";
                    }
                    if (city != null) {
                        title += city + ",";
                    }
                    if (state != null) {
                        title += state + ",";
                    }
                    if (country != null) {
                        title += country;
                    }
                    imgInfo.setText(imgDate_Time + "   " + Latitude + "   " + Longitude + "  "
                            + title);
                } else {
                    title = "Unknown Location";
                    imgInfo.setText("Address Not Found");
                }

            } else {
                if (imgDate_Time != null) {
                    imgInfo.setText(imgDate_Time);
                } else {
                    imgInfo.setText("Media Information Not Available");
                }

            }

        } catch (IOException e) {

            e.printStackTrace();
        }

        if (mediaFile.exists()) {

            if (isImage(mediaPath)) {
                displayVideo.setVisibility(View.GONE);
                displayImage.setVisibility(View.VISIBLE);
                Bitmap myBitmap = BitmapFactory.decodeFile(mediaFile.getAbsolutePath());
                int height = (myBitmap.getHeight() * 512 / myBitmap.getWidth());
                Bitmap scale = Bitmap.createScaledBitmap(myBitmap, 512, height, true);
                displayImage.setImageBitmap(scale);
            } else {
                displayImage.setVisibility(View.GONE);
                displayVideo.setVisibility(View.VISIBLE);

                displayVideo.setVideoURI(Uri.parse(mediaFile.toString()));
                displayVideo.start();
            }
        }
    }

    private Double convertToDegree(String stringDMS) {
        Double result = null;
        String[] DMS = stringDMS.split(",", 3);

        String[] stringD = DMS[0].split("/", 2);
        Double D0 = new Double(stringD[0]);
        Double D1 = new Double(stringD[1]);
        Double FloatD = D0 / D1;

        String[] stringM = DMS[1].split("/", 2);
        Double M0 = new Double(stringM[0]);
        Double M1 = new Double(stringM[1]);
        Double FloatM = M0 / M1;

        String[] stringS = DMS[2].split("/", 2);
        Double S0 = new Double(stringS[0]);
        Double S1 = new Double(stringS[1]);
        Double FloatS = S0 / S1;

        result = new Double(FloatD + (FloatM / 60) + (FloatS / 3600));

        return result;

    }

    public boolean isValid() {
        return valid;
    }

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return (String.valueOf(Latitude) + ", " + String.valueOf(Longitude));
    }

    public int getLatitudeE6() {
        return (int) (Latitude * 1000000);
    }

    public int getLongitudeE6() {
        return (int) (Longitude * 1000000);
    }

    public static boolean isImage(String str) {
        boolean temp = false;
        String[] arr = { ".jpeg", ".jpg", ".png", ".bmp", ".gif" };
        for (int i = 0; i < arr.length; i++) {
            temp = str.endsWith(arr[i]);
            if (temp) {
                break;
            }
        }
        return temp;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.splash_screen, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

2 个答案:

答案 0 :(得分:1)

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy:MM:dd hh:mm:ss");
    SimpleDateFormat convertDate = new SimpleDateFormat("MMM dd, yyyy");
    SimpleDateFormat convertTime = new SimpleDateFormat("hh:mm:ss aa");

    Date d = null, d2 = null;

    try {
         d = simpleDateFormat.parse(imgDate_Time);
         d2 = simpleDateFormat.parse(imgDate_Time);
   } catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   String dateFormat = convertDate.format(d);
   String timeFormat = convertTime.format(d2);


现在打印dateFormat和TimeFormat。和dateFormat和timeFormat是字符串变量。

答案 1 :(得分:0)

Chirag发布的用于将TAG解析为Joda时间对象的代码的重写版本:

    /**
     * Converts an ExifInterface time and date tag into a Joda time format
     *
     * @param EXIF_TAG_DATETIME
     * @return null in case of failure, the date object otherwise
     */
    public static LocalDateTime convert( String EXIF_TAG_DATETIME){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss", Locale.US);

        try {
            return new LocalDateTime( simpleDateFormat.parse( EXIF_TAG_DATETIME ) );
        } catch (ParseException e) {
            Log.e(TAG, e.getMessage());
            return null;
        }
    }