How to parse a date? [duplicate]











up vote
162
down vote

favorite
24













This question already has an answer here:




  • Java string to date conversion

    13 answers




I am trying to parse this date with SimpleDateFormat and it is not working:



import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Formaterclass {
public static void main(String args) throws ParseException{
String strDate = "Thu Jun 18 20:56:02 EDT 2009";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date dateStr = formatter.parse(strDate);
String formattedDate = formatter.format(dateStr);
System.out.println("yyyy-MM-dd date is ==>"+formattedDate);
Date date1 = formatter.parse(formattedDate);

formatter = new SimpleDateFormat("dd-MMM-yyyy");
formattedDate = formatter.format(date1);
System.out.println("dd-MMM-yyyy date is ==>"+formattedDate);
}
}


If I try this code with strDate="2008-10-14", I have a positive answer. What's the problem? How can I parse this format?



PS. I got this date from a jDatePicker and there is no instruction on how modify the date format I get when the user chooses a date.










share|improve this question















marked as duplicate by BalusC java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 17 '16 at 21:38


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 4




    For parsing/formatting dates with Java 8, have a look at this answer: stackoverflow.com/questions/22463062/…
    – micha
    Mar 30 '14 at 10:02















up vote
162
down vote

favorite
24













This question already has an answer here:




  • Java string to date conversion

    13 answers




I am trying to parse this date with SimpleDateFormat and it is not working:



import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Formaterclass {
public static void main(String args) throws ParseException{
String strDate = "Thu Jun 18 20:56:02 EDT 2009";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date dateStr = formatter.parse(strDate);
String formattedDate = formatter.format(dateStr);
System.out.println("yyyy-MM-dd date is ==>"+formattedDate);
Date date1 = formatter.parse(formattedDate);

formatter = new SimpleDateFormat("dd-MMM-yyyy");
formattedDate = formatter.format(date1);
System.out.println("dd-MMM-yyyy date is ==>"+formattedDate);
}
}


If I try this code with strDate="2008-10-14", I have a positive answer. What's the problem? How can I parse this format?



PS. I got this date from a jDatePicker and there is no instruction on how modify the date format I get when the user chooses a date.










share|improve this question















marked as duplicate by BalusC java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 17 '16 at 21:38


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 4




    For parsing/formatting dates with Java 8, have a look at this answer: stackoverflow.com/questions/22463062/…
    – micha
    Mar 30 '14 at 10:02













up vote
162
down vote

favorite
24









up vote
162
down vote

favorite
24






24






This question already has an answer here:




  • Java string to date conversion

    13 answers




I am trying to parse this date with SimpleDateFormat and it is not working:



import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Formaterclass {
public static void main(String args) throws ParseException{
String strDate = "Thu Jun 18 20:56:02 EDT 2009";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date dateStr = formatter.parse(strDate);
String formattedDate = formatter.format(dateStr);
System.out.println("yyyy-MM-dd date is ==>"+formattedDate);
Date date1 = formatter.parse(formattedDate);

formatter = new SimpleDateFormat("dd-MMM-yyyy");
formattedDate = formatter.format(date1);
System.out.println("dd-MMM-yyyy date is ==>"+formattedDate);
}
}


If I try this code with strDate="2008-10-14", I have a positive answer. What's the problem? How can I parse this format?



PS. I got this date from a jDatePicker and there is no instruction on how modify the date format I get when the user chooses a date.










share|improve this question
















This question already has an answer here:




  • Java string to date conversion

    13 answers




I am trying to parse this date with SimpleDateFormat and it is not working:



import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Formaterclass {
public static void main(String args) throws ParseException{
String strDate = "Thu Jun 18 20:56:02 EDT 2009";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date dateStr = formatter.parse(strDate);
String formattedDate = formatter.format(dateStr);
System.out.println("yyyy-MM-dd date is ==>"+formattedDate);
Date date1 = formatter.parse(formattedDate);

formatter = new SimpleDateFormat("dd-MMM-yyyy");
formattedDate = formatter.format(date1);
System.out.println("dd-MMM-yyyy date is ==>"+formattedDate);
}
}


If I try this code with strDate="2008-10-14", I have a positive answer. What's the problem? How can I parse this format?



PS. I got this date from a jDatePicker and there is no instruction on how modify the date format I get when the user chooses a date.





This question already has an answer here:




  • Java string to date conversion

    13 answers








java date simpledateformat






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jul 1 '13 at 17:12









Nathaniel Ford

13.3k145271




13.3k145271










asked Jun 16 '09 at 1:01









fenec

2,35284175




2,35284175




marked as duplicate by BalusC java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 17 '16 at 21:38


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by BalusC java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 17 '16 at 21:38


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 4




    For parsing/formatting dates with Java 8, have a look at this answer: stackoverflow.com/questions/22463062/…
    – micha
    Mar 30 '14 at 10:02














  • 4




    For parsing/formatting dates with Java 8, have a look at this answer: stackoverflow.com/questions/22463062/…
    – micha
    Mar 30 '14 at 10:02








4




4




For parsing/formatting dates with Java 8, have a look at this answer: stackoverflow.com/questions/22463062/…
– micha
Mar 30 '14 at 10:02




For parsing/formatting dates with Java 8, have a look at this answer: stackoverflow.com/questions/22463062/…
– micha
Mar 30 '14 at 10:02












5 Answers
5






active

oldest

votes

















up vote
219
down vote



accepted










You cannot expect to parse a date with a SimpleDateFormat that is set up with a different format.



To parse your "Thu Jun 18 20:56:02 EDT 2009" date string you need a SimpleDateFormat like this (roughly):



SimpleDateFormat parser=new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");


Use this to parse the string into a Date, and then your other SimpleDateFormat to turn that Date into the format you want.



        String input = "Thu Jun 18 20:56:02 EDT 2009";
SimpleDateFormat parser = new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");
Date date = parser.parse(input);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = formatter.format(date);

...


JavaDoc: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html






share|improve this answer























  • having the same issue ... it works good on Java7, but fails on parsing "Thu Jun ..." as "EEE MMM ..." in Java6. Any ideas ?
    – d.raev
    Apr 1 '13 at 11:47








  • 11




    eventuell new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy",Locale.ENGLISH); could be needed if current locale != ENGLISH
    – Nightingale7
    Apr 22 '13 at 13:44


















up vote
51
down vote













The problem is that you have a date formatted like this:



Thu Jun 18 20:56:02 EDT 2009


But are using a SimpleDateFormat that is:



yyyy-MM-dd


The two formats don't agree. You need to construct a SimpleDateFormat that matches the layout of the string you're trying to parse into a Date. Lining things up to make it easy to see, you want a SimpleDateFormat like this:



EEE MMM dd HH:mm:ss zzz yyyy
Thu Jun 18 20:56:02 EDT 2009


Check the JavaDoc page I linked to and see how the characters are used.






share|improve this answer



















  • 1




    I really wanted to up-vote this but you have exactly 42 up-votes, sorry!
    – Madbreaks
    Jun 6 '16 at 17:56


















up vote
15
down vote













We now have a more modern way to do this work.



java.time



The java.time framework is bundled with Java 8 and later. See Tutorial. These new classes are inspired by Joda-Time, defined by JSR 310, and extended by the ThreeTen-Extra project. They are a vast improvement over the troublesome old classes, java.util.Date/.Calendar et al.



Note that the 3-4 letter codes like EDT are neither standardized nor unique. Avoid them whenever possible. Learn to use ISO 8601 standard formats instead. The java.time framework may take a stab at translating, but many of the commonly used codes have duplicate values.



By the way, note how java.time by default generates strings using the ISO 8601 formats but extended by appending the name of the time zone in brackets.



String input = "Thu Jun 18 20:56:02 EDT 2009";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern ( "EEE MMM d HH:mm:ss zzz yyyy" , Locale.ENGLISH );
ZonedDateTime zdt = formatter.parse ( input , ZonedDateTime :: from );


Dump to console.



System.out.println ( "zdt : " + zdt );


When run.




zdt : 2009-06-18T20:56:02-04:00[America/New_York]




Adjust Time Zone



For fun let's adjust to the India time zone.



ZonedDateTime zdtKolkata = zdt.withZoneSameInstant ( ZoneId.of ( "Asia/Kolkata" ) );



zdtKolkata : 2009-06-19T06:26:02+05:30[Asia/Kolkata]




Convert to j.u.Date



If you really need a java.util.Date object for use with classes not yet updated to the java.time types, convert. Note that you are losing the assigned time zone, but have the same moment automatically adjusted to UTC.



java.util.Date date = java.util.Date.from( zdt.toInstant() );





share|improve this answer






























    up vote
    12
    down vote













    How about getSelectedDate? Anyway, specifically on your code question, the problem is with this line:



    new SimpleDateFormat("yyyy-MM-dd");


    The string that goes in the constructor has to match the format of the date. The documentation for how to do that is here. Looks like you need something close to "EEE MMM d HH:mm:ss zzz yyyy"






    share|improve this answer




























      up vote
      1
      down vote













      In response to:
      "How to convert Tue Sep 13 2016 00:00:00 GMT-0500 (Hora de verano central (México)) to dd-MM-yy in Java?", it was marked how duplicate



      Try this:
      With java.util.Date, java.text.SimpleDateFormat, it's a simple solution.



      public static void main(String args) throws ParseException {

      String fecha = "Tue Sep 13 2016 00:00:00 GMT-0500 (Hora de verano central (México))";
      Date f = new Date(fecha);

      SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
      sdf.setTimeZone(TimeZone.getTimeZone("-5GMT"));
      fecha = sdf.format(f);
      System.out.println(fecha);
      }





      share|improve this answer






























        5 Answers
        5






        active

        oldest

        votes








        5 Answers
        5






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        219
        down vote



        accepted










        You cannot expect to parse a date with a SimpleDateFormat that is set up with a different format.



        To parse your "Thu Jun 18 20:56:02 EDT 2009" date string you need a SimpleDateFormat like this (roughly):



        SimpleDateFormat parser=new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");


        Use this to parse the string into a Date, and then your other SimpleDateFormat to turn that Date into the format you want.



                String input = "Thu Jun 18 20:56:02 EDT 2009";
        SimpleDateFormat parser = new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");
        Date date = parser.parse(input);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String formattedDate = formatter.format(date);

        ...


        JavaDoc: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html






        share|improve this answer























        • having the same issue ... it works good on Java7, but fails on parsing "Thu Jun ..." as "EEE MMM ..." in Java6. Any ideas ?
          – d.raev
          Apr 1 '13 at 11:47








        • 11




          eventuell new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy",Locale.ENGLISH); could be needed if current locale != ENGLISH
          – Nightingale7
          Apr 22 '13 at 13:44















        up vote
        219
        down vote



        accepted










        You cannot expect to parse a date with a SimpleDateFormat that is set up with a different format.



        To parse your "Thu Jun 18 20:56:02 EDT 2009" date string you need a SimpleDateFormat like this (roughly):



        SimpleDateFormat parser=new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");


        Use this to parse the string into a Date, and then your other SimpleDateFormat to turn that Date into the format you want.



                String input = "Thu Jun 18 20:56:02 EDT 2009";
        SimpleDateFormat parser = new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");
        Date date = parser.parse(input);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String formattedDate = formatter.format(date);

        ...


        JavaDoc: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html






        share|improve this answer























        • having the same issue ... it works good on Java7, but fails on parsing "Thu Jun ..." as "EEE MMM ..." in Java6. Any ideas ?
          – d.raev
          Apr 1 '13 at 11:47








        • 11




          eventuell new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy",Locale.ENGLISH); could be needed if current locale != ENGLISH
          – Nightingale7
          Apr 22 '13 at 13:44













        up vote
        219
        down vote



        accepted







        up vote
        219
        down vote



        accepted






        You cannot expect to parse a date with a SimpleDateFormat that is set up with a different format.



        To parse your "Thu Jun 18 20:56:02 EDT 2009" date string you need a SimpleDateFormat like this (roughly):



        SimpleDateFormat parser=new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");


        Use this to parse the string into a Date, and then your other SimpleDateFormat to turn that Date into the format you want.



                String input = "Thu Jun 18 20:56:02 EDT 2009";
        SimpleDateFormat parser = new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");
        Date date = parser.parse(input);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String formattedDate = formatter.format(date);

        ...


        JavaDoc: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html






        share|improve this answer














        You cannot expect to parse a date with a SimpleDateFormat that is set up with a different format.



        To parse your "Thu Jun 18 20:56:02 EDT 2009" date string you need a SimpleDateFormat like this (roughly):



        SimpleDateFormat parser=new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");


        Use this to parse the string into a Date, and then your other SimpleDateFormat to turn that Date into the format you want.



                String input = "Thu Jun 18 20:56:02 EDT 2009";
        SimpleDateFormat parser = new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");
        Date date = parser.parse(input);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String formattedDate = formatter.format(date);

        ...


        JavaDoc: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Sep 30 '16 at 21:46

























        answered Jun 16 '09 at 1:09









        jwl

        6,478123979




        6,478123979












        • having the same issue ... it works good on Java7, but fails on parsing "Thu Jun ..." as "EEE MMM ..." in Java6. Any ideas ?
          – d.raev
          Apr 1 '13 at 11:47








        • 11




          eventuell new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy",Locale.ENGLISH); could be needed if current locale != ENGLISH
          – Nightingale7
          Apr 22 '13 at 13:44


















        • having the same issue ... it works good on Java7, but fails on parsing "Thu Jun ..." as "EEE MMM ..." in Java6. Any ideas ?
          – d.raev
          Apr 1 '13 at 11:47








        • 11




          eventuell new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy",Locale.ENGLISH); could be needed if current locale != ENGLISH
          – Nightingale7
          Apr 22 '13 at 13:44
















        having the same issue ... it works good on Java7, but fails on parsing "Thu Jun ..." as "EEE MMM ..." in Java6. Any ideas ?
        – d.raev
        Apr 1 '13 at 11:47






        having the same issue ... it works good on Java7, but fails on parsing "Thu Jun ..." as "EEE MMM ..." in Java6. Any ideas ?
        – d.raev
        Apr 1 '13 at 11:47






        11




        11




        eventuell new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy",Locale.ENGLISH); could be needed if current locale != ENGLISH
        – Nightingale7
        Apr 22 '13 at 13:44




        eventuell new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy",Locale.ENGLISH); could be needed if current locale != ENGLISH
        – Nightingale7
        Apr 22 '13 at 13:44












        up vote
        51
        down vote













        The problem is that you have a date formatted like this:



        Thu Jun 18 20:56:02 EDT 2009


        But are using a SimpleDateFormat that is:



        yyyy-MM-dd


        The two formats don't agree. You need to construct a SimpleDateFormat that matches the layout of the string you're trying to parse into a Date. Lining things up to make it easy to see, you want a SimpleDateFormat like this:



        EEE MMM dd HH:mm:ss zzz yyyy
        Thu Jun 18 20:56:02 EDT 2009


        Check the JavaDoc page I linked to and see how the characters are used.






        share|improve this answer



















        • 1




          I really wanted to up-vote this but you have exactly 42 up-votes, sorry!
          – Madbreaks
          Jun 6 '16 at 17:56















        up vote
        51
        down vote













        The problem is that you have a date formatted like this:



        Thu Jun 18 20:56:02 EDT 2009


        But are using a SimpleDateFormat that is:



        yyyy-MM-dd


        The two formats don't agree. You need to construct a SimpleDateFormat that matches the layout of the string you're trying to parse into a Date. Lining things up to make it easy to see, you want a SimpleDateFormat like this:



        EEE MMM dd HH:mm:ss zzz yyyy
        Thu Jun 18 20:56:02 EDT 2009


        Check the JavaDoc page I linked to and see how the characters are used.






        share|improve this answer



















        • 1




          I really wanted to up-vote this but you have exactly 42 up-votes, sorry!
          – Madbreaks
          Jun 6 '16 at 17:56













        up vote
        51
        down vote










        up vote
        51
        down vote









        The problem is that you have a date formatted like this:



        Thu Jun 18 20:56:02 EDT 2009


        But are using a SimpleDateFormat that is:



        yyyy-MM-dd


        The two formats don't agree. You need to construct a SimpleDateFormat that matches the layout of the string you're trying to parse into a Date. Lining things up to make it easy to see, you want a SimpleDateFormat like this:



        EEE MMM dd HH:mm:ss zzz yyyy
        Thu Jun 18 20:56:02 EDT 2009


        Check the JavaDoc page I linked to and see how the characters are used.






        share|improve this answer














        The problem is that you have a date formatted like this:



        Thu Jun 18 20:56:02 EDT 2009


        But are using a SimpleDateFormat that is:



        yyyy-MM-dd


        The two formats don't agree. You need to construct a SimpleDateFormat that matches the layout of the string you're trying to parse into a Date. Lining things up to make it easy to see, you want a SimpleDateFormat like this:



        EEE MMM dd HH:mm:ss zzz yyyy
        Thu Jun 18 20:56:02 EDT 2009


        Check the JavaDoc page I linked to and see how the characters are used.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Dec 19 '15 at 9:50









        Abdelhak

        7,69631331




        7,69631331










        answered Jun 16 '09 at 1:08









        Eddie

        44.9k18105135




        44.9k18105135








        • 1




          I really wanted to up-vote this but you have exactly 42 up-votes, sorry!
          – Madbreaks
          Jun 6 '16 at 17:56














        • 1




          I really wanted to up-vote this but you have exactly 42 up-votes, sorry!
          – Madbreaks
          Jun 6 '16 at 17:56








        1




        1




        I really wanted to up-vote this but you have exactly 42 up-votes, sorry!
        – Madbreaks
        Jun 6 '16 at 17:56




        I really wanted to up-vote this but you have exactly 42 up-votes, sorry!
        – Madbreaks
        Jun 6 '16 at 17:56










        up vote
        15
        down vote













        We now have a more modern way to do this work.



        java.time



        The java.time framework is bundled with Java 8 and later. See Tutorial. These new classes are inspired by Joda-Time, defined by JSR 310, and extended by the ThreeTen-Extra project. They are a vast improvement over the troublesome old classes, java.util.Date/.Calendar et al.



        Note that the 3-4 letter codes like EDT are neither standardized nor unique. Avoid them whenever possible. Learn to use ISO 8601 standard formats instead. The java.time framework may take a stab at translating, but many of the commonly used codes have duplicate values.



        By the way, note how java.time by default generates strings using the ISO 8601 formats but extended by appending the name of the time zone in brackets.



        String input = "Thu Jun 18 20:56:02 EDT 2009";
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern ( "EEE MMM d HH:mm:ss zzz yyyy" , Locale.ENGLISH );
        ZonedDateTime zdt = formatter.parse ( input , ZonedDateTime :: from );


        Dump to console.



        System.out.println ( "zdt : " + zdt );


        When run.




        zdt : 2009-06-18T20:56:02-04:00[America/New_York]




        Adjust Time Zone



        For fun let's adjust to the India time zone.



        ZonedDateTime zdtKolkata = zdt.withZoneSameInstant ( ZoneId.of ( "Asia/Kolkata" ) );



        zdtKolkata : 2009-06-19T06:26:02+05:30[Asia/Kolkata]




        Convert to j.u.Date



        If you really need a java.util.Date object for use with classes not yet updated to the java.time types, convert. Note that you are losing the assigned time zone, but have the same moment automatically adjusted to UTC.



        java.util.Date date = java.util.Date.from( zdt.toInstant() );





        share|improve this answer



























          up vote
          15
          down vote













          We now have a more modern way to do this work.



          java.time



          The java.time framework is bundled with Java 8 and later. See Tutorial. These new classes are inspired by Joda-Time, defined by JSR 310, and extended by the ThreeTen-Extra project. They are a vast improvement over the troublesome old classes, java.util.Date/.Calendar et al.



          Note that the 3-4 letter codes like EDT are neither standardized nor unique. Avoid them whenever possible. Learn to use ISO 8601 standard formats instead. The java.time framework may take a stab at translating, but many of the commonly used codes have duplicate values.



          By the way, note how java.time by default generates strings using the ISO 8601 formats but extended by appending the name of the time zone in brackets.



          String input = "Thu Jun 18 20:56:02 EDT 2009";
          DateTimeFormatter formatter = DateTimeFormatter.ofPattern ( "EEE MMM d HH:mm:ss zzz yyyy" , Locale.ENGLISH );
          ZonedDateTime zdt = formatter.parse ( input , ZonedDateTime :: from );


          Dump to console.



          System.out.println ( "zdt : " + zdt );


          When run.




          zdt : 2009-06-18T20:56:02-04:00[America/New_York]




          Adjust Time Zone



          For fun let's adjust to the India time zone.



          ZonedDateTime zdtKolkata = zdt.withZoneSameInstant ( ZoneId.of ( "Asia/Kolkata" ) );



          zdtKolkata : 2009-06-19T06:26:02+05:30[Asia/Kolkata]




          Convert to j.u.Date



          If you really need a java.util.Date object for use with classes not yet updated to the java.time types, convert. Note that you are losing the assigned time zone, but have the same moment automatically adjusted to UTC.



          java.util.Date date = java.util.Date.from( zdt.toInstant() );





          share|improve this answer

























            up vote
            15
            down vote










            up vote
            15
            down vote









            We now have a more modern way to do this work.



            java.time



            The java.time framework is bundled with Java 8 and later. See Tutorial. These new classes are inspired by Joda-Time, defined by JSR 310, and extended by the ThreeTen-Extra project. They are a vast improvement over the troublesome old classes, java.util.Date/.Calendar et al.



            Note that the 3-4 letter codes like EDT are neither standardized nor unique. Avoid them whenever possible. Learn to use ISO 8601 standard formats instead. The java.time framework may take a stab at translating, but many of the commonly used codes have duplicate values.



            By the way, note how java.time by default generates strings using the ISO 8601 formats but extended by appending the name of the time zone in brackets.



            String input = "Thu Jun 18 20:56:02 EDT 2009";
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern ( "EEE MMM d HH:mm:ss zzz yyyy" , Locale.ENGLISH );
            ZonedDateTime zdt = formatter.parse ( input , ZonedDateTime :: from );


            Dump to console.



            System.out.println ( "zdt : " + zdt );


            When run.




            zdt : 2009-06-18T20:56:02-04:00[America/New_York]




            Adjust Time Zone



            For fun let's adjust to the India time zone.



            ZonedDateTime zdtKolkata = zdt.withZoneSameInstant ( ZoneId.of ( "Asia/Kolkata" ) );



            zdtKolkata : 2009-06-19T06:26:02+05:30[Asia/Kolkata]




            Convert to j.u.Date



            If you really need a java.util.Date object for use with classes not yet updated to the java.time types, convert. Note that you are losing the assigned time zone, but have the same moment automatically adjusted to UTC.



            java.util.Date date = java.util.Date.from( zdt.toInstant() );





            share|improve this answer














            We now have a more modern way to do this work.



            java.time



            The java.time framework is bundled with Java 8 and later. See Tutorial. These new classes are inspired by Joda-Time, defined by JSR 310, and extended by the ThreeTen-Extra project. They are a vast improvement over the troublesome old classes, java.util.Date/.Calendar et al.



            Note that the 3-4 letter codes like EDT are neither standardized nor unique. Avoid them whenever possible. Learn to use ISO 8601 standard formats instead. The java.time framework may take a stab at translating, but many of the commonly used codes have duplicate values.



            By the way, note how java.time by default generates strings using the ISO 8601 formats but extended by appending the name of the time zone in brackets.



            String input = "Thu Jun 18 20:56:02 EDT 2009";
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern ( "EEE MMM d HH:mm:ss zzz yyyy" , Locale.ENGLISH );
            ZonedDateTime zdt = formatter.parse ( input , ZonedDateTime :: from );


            Dump to console.



            System.out.println ( "zdt : " + zdt );


            When run.




            zdt : 2009-06-18T20:56:02-04:00[America/New_York]




            Adjust Time Zone



            For fun let's adjust to the India time zone.



            ZonedDateTime zdtKolkata = zdt.withZoneSameInstant ( ZoneId.of ( "Asia/Kolkata" ) );



            zdtKolkata : 2009-06-19T06:26:02+05:30[Asia/Kolkata]




            Convert to j.u.Date



            If you really need a java.util.Date object for use with classes not yet updated to the java.time types, convert. Note that you are losing the assigned time zone, but have the same moment automatically adjusted to UTC.



            java.util.Date date = java.util.Date.from( zdt.toInstant() );






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Oct 11 '15 at 1:52

























            answered Oct 11 '15 at 1:08









            Basil Bourque

            102k25353510




            102k25353510






















                up vote
                12
                down vote













                How about getSelectedDate? Anyway, specifically on your code question, the problem is with this line:



                new SimpleDateFormat("yyyy-MM-dd");


                The string that goes in the constructor has to match the format of the date. The documentation for how to do that is here. Looks like you need something close to "EEE MMM d HH:mm:ss zzz yyyy"






                share|improve this answer

























                  up vote
                  12
                  down vote













                  How about getSelectedDate? Anyway, specifically on your code question, the problem is with this line:



                  new SimpleDateFormat("yyyy-MM-dd");


                  The string that goes in the constructor has to match the format of the date. The documentation for how to do that is here. Looks like you need something close to "EEE MMM d HH:mm:ss zzz yyyy"






                  share|improve this answer























                    up vote
                    12
                    down vote










                    up vote
                    12
                    down vote









                    How about getSelectedDate? Anyway, specifically on your code question, the problem is with this line:



                    new SimpleDateFormat("yyyy-MM-dd");


                    The string that goes in the constructor has to match the format of the date. The documentation for how to do that is here. Looks like you need something close to "EEE MMM d HH:mm:ss zzz yyyy"






                    share|improve this answer












                    How about getSelectedDate? Anyway, specifically on your code question, the problem is with this line:



                    new SimpleDateFormat("yyyy-MM-dd");


                    The string that goes in the constructor has to match the format of the date. The documentation for how to do that is here. Looks like you need something close to "EEE MMM d HH:mm:ss zzz yyyy"







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jun 16 '09 at 1:10









                    Yishai

                    71k20158237




                    71k20158237






















                        up vote
                        1
                        down vote













                        In response to:
                        "How to convert Tue Sep 13 2016 00:00:00 GMT-0500 (Hora de verano central (México)) to dd-MM-yy in Java?", it was marked how duplicate



                        Try this:
                        With java.util.Date, java.text.SimpleDateFormat, it's a simple solution.



                        public static void main(String args) throws ParseException {

                        String fecha = "Tue Sep 13 2016 00:00:00 GMT-0500 (Hora de verano central (México))";
                        Date f = new Date(fecha);

                        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
                        sdf.setTimeZone(TimeZone.getTimeZone("-5GMT"));
                        fecha = sdf.format(f);
                        System.out.println(fecha);
                        }





                        share|improve this answer



























                          up vote
                          1
                          down vote













                          In response to:
                          "How to convert Tue Sep 13 2016 00:00:00 GMT-0500 (Hora de verano central (México)) to dd-MM-yy in Java?", it was marked how duplicate



                          Try this:
                          With java.util.Date, java.text.SimpleDateFormat, it's a simple solution.



                          public static void main(String args) throws ParseException {

                          String fecha = "Tue Sep 13 2016 00:00:00 GMT-0500 (Hora de verano central (México))";
                          Date f = new Date(fecha);

                          SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
                          sdf.setTimeZone(TimeZone.getTimeZone("-5GMT"));
                          fecha = sdf.format(f);
                          System.out.println(fecha);
                          }





                          share|improve this answer

























                            up vote
                            1
                            down vote










                            up vote
                            1
                            down vote









                            In response to:
                            "How to convert Tue Sep 13 2016 00:00:00 GMT-0500 (Hora de verano central (México)) to dd-MM-yy in Java?", it was marked how duplicate



                            Try this:
                            With java.util.Date, java.text.SimpleDateFormat, it's a simple solution.



                            public static void main(String args) throws ParseException {

                            String fecha = "Tue Sep 13 2016 00:00:00 GMT-0500 (Hora de verano central (México))";
                            Date f = new Date(fecha);

                            SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
                            sdf.setTimeZone(TimeZone.getTimeZone("-5GMT"));
                            fecha = sdf.format(f);
                            System.out.println(fecha);
                            }





                            share|improve this answer














                            In response to:
                            "How to convert Tue Sep 13 2016 00:00:00 GMT-0500 (Hora de verano central (México)) to dd-MM-yy in Java?", it was marked how duplicate



                            Try this:
                            With java.util.Date, java.text.SimpleDateFormat, it's a simple solution.



                            public static void main(String args) throws ParseException {

                            String fecha = "Tue Sep 13 2016 00:00:00 GMT-0500 (Hora de verano central (México))";
                            Date f = new Date(fecha);

                            SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
                            sdf.setTimeZone(TimeZone.getTimeZone("-5GMT"));
                            fecha = sdf.format(f);
                            System.out.println(fecha);
                            }






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Sep 6 '16 at 5:45

























                            answered Sep 6 '16 at 5:29









                            Edgar Civil

                            114




                            114















                                Popular posts from this blog

                                404 Error Contact Form 7 ajax form submitting

                                How to know if a Active Directory user can login interactively

                                TypeError: fit_transform() missing 1 required positional argument: 'X'