Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the given date string format(pattern) in java?

Tags:

java

date

format

I want to get the format of a given date string.

Example: I have a string like 2011-09-27T07:04:21.97-05:00 and the date format of this string is yyyy-MM-dd'T'HH:mm:ss.SSS.

Here I want to find out this date format when I pass string(2011-09-27T07:04:21.97-05:00) to a method which will return the format(yyyy-MM-dd'T'HH:mm:ss.SSS), then later I will format my given date string according to my requirement(like yy-mm--dd or mm/dd/yyyy).

Can any one tell me how can I get it achieved?

like image 710
sathish Avatar asked Sep 28 '11 06:09

sathish


People also ask

How do you check if the date is in YYYY-MM-DD format in Java?

DateValidator validator = new DateValidatorUsingDateFormat("MM/dd/yyyy"); assertTrue(validator. isValid("02/28/2019")); assertFalse(validator. isValid("02/30/2019"));

How do I check if a string is a specific date format?

Show activity on this post. DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); formatter. setLenient(false); try { Date date= formatter. parse("02/03/2010"); } catch (ParseException e) { //If input date is in different format or invalid. }

How do I print a date in a specific format?

Formatting Dates String pattern = "yyyy-MM-dd"; SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern); String date = simpleDateFormat. format(new Date()); System. out. println(date);


2 Answers

import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.regex.Matcher; import java.util.regex.Pattern;  public class NewClass {      private static final String[] formats = {                  "yyyy-MM-dd'T'HH:mm:ss'Z'",   "yyyy-MM-dd'T'HH:mm:ssZ",                 "yyyy-MM-dd'T'HH:mm:ss",      "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",                 "yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd HH:mm:ss",                  "MM/dd/yyyy HH:mm:ss",        "MM/dd/yyyy'T'HH:mm:ss.SSS'Z'",                  "MM/dd/yyyy'T'HH:mm:ss.SSSZ", "MM/dd/yyyy'T'HH:mm:ss.SSS",                  "MM/dd/yyyy'T'HH:mm:ssZ",     "MM/dd/yyyy'T'HH:mm:ss",                  "yyyy:MM:dd HH:mm:ss",        "yyyyMMdd", };          /*          * @param args          */     public static void main(String[] args) {         String yyyyMMdd = "20110917";            parse(yyyyMMdd);     }      public static void parse(String d) {         if (d != null) {             for (String parse : formats) {                 SimpleDateFormat sdf = new SimpleDateFormat(parse);                 try {                     sdf.parse(d);                     System.out.println("Printing the value of " + parse);                 } catch (ParseException e) {                  }             }         }     } } 
like image 131
Madhu Avatar answered Sep 23 '22 05:09

Madhu


you can do like this way, I don't know good way or not but try this

first create the SimpleDateFormat object

SimpleDateFormt sdf = new SimpleDateFormat("yyyy-MM-dd 'T' HH:mm:ss.SSS"); 

now when check the date if this will parse in this format then change as per your format

try{      Date date = sdf.parse(yourdate);      sdf.applyPattern("yy-mm--dd or mm/dd/yyyy");      String dateformat = sdf.format(date); }catch(Exception ex) { // here forgot the exact exception class Parse exception was used     // do something here } 

updated post:

Returning a date format from an unknown format of date string in java

How to convert String to Date without knowing the format?

Parse any date in Java

like image 24
Pratik Avatar answered Sep 22 '22 05:09

Pratik