Example usage for java.text SimpleDateFormat parse

List of usage examples for java.text SimpleDateFormat parse

Introduction

In this page you can find the example usage for java.text SimpleDateFormat parse.

Prototype

public Date parse(String source) throws ParseException 

Source Link

Document

Parses text from the beginning of the given string to produce a date.

Usage

From source file:Main.java

public static Date String2Date(String date) {
    Date DateTime = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINESE);
    try {/*from  w  w  w . j  a v  a  2s.  c o  m*/
        DateTime = sdf.parse(date);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return DateTime;
}

From source file:Main.java

public static int getDiffHour(String startTime, String endTime) {
    long diff = 0;
    SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    try {//from  ww  w  .  j  a  va 2 s.  c  o  m
        Date startDate = ft.parse(startTime);
        Date endDate = ft.parse(endTime);
        diff = startDate.getTime() - endDate.getTime();
        diff = diff / (1000 * 60 * 60);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return new Long(diff).intValue();
}

From source file:Main.java

public static Date stringToDate(String dateString) {
    SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm", Locale.ENGLISH);
    try {/*from  www.  j  a v  a 2s  .  c o m*/
        return format.parse(dateString);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static long getDiff(String startTime, String endTime) {
    long diff = 0;
    SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    try {//from www .  ja  v  a 2  s  .c o m
        Date startDate = ft.parse(startTime);
        Date endDate = ft.parse(endTime);
        diff = startDate.getTime() - endDate.getTime();
        diff = diff / 1000;
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return diff;
}

From source file:Main.java

public static Date getFormatDate2(String date) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    Date date0 = null;// w w w.  ja v  a  2 s. c o  m
    try {
        date0 = sdf.parse(date);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return date0;
}

From source file:com.searchbox.utils.DecryptLicense.java

private static boolean checkDate(String dateString) {
    try {/*  w  w w .ja  va2 s .  c o  m*/
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.parse(dateString).after(new Date());
    } catch (ParseException ex) {
        Logger.getLogger(DecryptLicense.class.getName()).log(Level.SEVERE, null, ex);
    }
    return false;
}

From source file:it.inserpio.mapillary.gopro.importer.exif.EXIFPropertyReader.java

public static long getDateTimeOriginal(File imageFile) throws ImageReadException, IOException, ParseException {
    final IImageMetadata metadata = Imaging.getMetadata(imageFile);
    final JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata;

    String dateTimeOriginal = jpegMetadata.getExif()
            .getFieldValue(ExifTagConstants.EXIF_TAG_DATE_TIME_ORIGINAL)[0];

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");

    return dateFormat.parse(dateTimeOriginal).getTime();
}

From source file:Main.java

public static Date parseDate(String tp) {
    //2008-03-10 04:32
    String dateFormat = "EEE MMM dd HH:mm:ss zzz yyyy";
    SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
    try {/*from w  w  w. ja v a  2s.  com*/
        return sdf.parse(tp);
    } catch (ParseException e) {
        String dateFormat2 = "yyyy-mm-dd HH:mm";
        SimpleDateFormat sdf2 = new SimpleDateFormat(dateFormat2);
        try {
            return sdf2.parse(tp);
        } catch (ParseException e1) {
            String dateFormat3 = "dd MMM yyyy, HH:mm";
            SimpleDateFormat sdf3 = new SimpleDateFormat(dateFormat3);
            try {
                return sdf3.parse(tp);
            } catch (ParseException e2) {
                e1.printStackTrace();
            }
        }
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static int monthsBetweenDates(String start, String end) throws ParseException {
    SimpleDateFormat format = new SimpleDateFormat("MMMM, yyyy", Locale.getDefault());
    Date startDate = format.parse(start);
    Date endDate = format.parse(end);

    Calendar startCalendar = new GregorianCalendar();
    startCalendar.setTime(startDate);//from   w ww.ja v a2 s . c  o  m
    Calendar endCalendar = new GregorianCalendar();
    endCalendar.setTime(endDate);

    int diffYear = endCalendar.get(Calendar.YEAR) - startCalendar.get(Calendar.YEAR);
    return diffYear * 12 + endCalendar.get(Calendar.MONTH) - startCalendar.get(Calendar.MONTH);
}

From source file:Main.java

public static Date getDateFromString(String dateStr, String pattern) {
    SimpleDateFormat sdf = new SimpleDateFormat(pattern);
    Date resDate = null;/*from w w  w  .j ava 2s  .  com*/
    try {
        resDate = sdf.parse(dateStr);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return resDate;
}