java - How to convert String into Date object using "DateFormat"? -


I am trying to change the date / time of my machine in the GMT timezone by using the following code: Date Format GMTformat = New SimpleDateform ("EMM DD HH: MM: SS GMT 'yay)"; Timezone GMTtime = Timezone.Gate Time Zone ("GMT"); gmtFormat.setTimeZone (gmtTime); System.out.println ("Original Date:" + New Date ()); String s = gmtFormat.format (new date ()); System.out.println ("as the converted date string:" + s); System.out.println ("Date changed as Date object:" + GMTFormat.parse (S));

Code output above:

  Normal date: Mon 21 21:04:06 AST 2014 Converted date as string: Mon 21 Apr 18:04 : 06 GMT 2014 As a converted date date object: Mon 21 21:04:06 AST 2014   

However my problem is when I have a "converted date string" as "converged date string" Object "using the pars function, but as you can see the date changes to the original date

Why is that?


The problem is solved when I do the following:

  date format gmtFormat = new SimpleDateform ("EMM DD HH: mm: SS 'GMT' Yay "); Timezone GMTtime = Timezone.Gate Time Zone ("GMT"); gmtFormat.setTimeZone (gmtTime); System.out.println ("Original Date:" + New Date ()); String s = gmtFormat.format (new date ()); System.out.println ("as the converted date string:" + s); // I just added the local time zone format and I use it to make a parsing call instead of the GMTFormat DateFormat LocalFormat = New SimpleDateFormat ("E MMM dd HH: mm: ss' GMT 'yyyy'); System.out.println ("Date changed as the date object:" + local format. Peers (s));   

I'm wondering why this happens & gt; & Gt; How do the local format related to parsing the function? Return value from dateFormat.parse (string)

code> a java.util .Date is not , which does not contain any information about time zone or locale. A date is essentially a cover item around the long value representing several milliseconds from January 1, 1970 to 00:00 GMT. When its toString () method applies, the default implementation presents the date in your local system's default timezone.

If you want to represent a date / time that stores both timestamps and in an associated time zone / locale, you get a java.util.calendar should use the object ( calendar.getInstance (...) and provide it with the timezone and / or locale , Then set the time associated with the calendar with the date object representing the date / time you want Not.

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -