2

I am trying to convert a string date into util.Date format and i have tried this method as follows

String startingDate="25 Mar 2016";
SimpleDateFormat sdf=new SimpleDateFormat("dd MMM yyyy");
sdf.setTimeZone(TimeZone.getTimeZone("IST"));
Date date=sdf.parse(startingDate);

I wanted the date output to be "Fri Mar 25 00:00:00 IST 2016" but i am getting "Thu Mar 24 18:30:00 UTC 2016" which is not correct because i am getting different TIMEZONE and i want "IST" Timezone. Please help ! Thanx in advance

3
  • If you are converting it to Date object then you'll have time also. Why you are converting the String to Date when you want the String in the first place? Commented Mar 25, 2016 at 7:40
  • No matter if i get time but i need that in IST format the output is in UTC format and i dont need that. And i want that in date because i want to use date.getTime() method available in date. Commented Mar 25, 2016 at 7:44
  • If I understand your problem correctly you want the console to print the Date in IST format instead of UTC. Check my answer below. Commented Mar 25, 2016 at 7:58

4 Answers 4

3

You have already converted the TimeZone to IST by setting the TimeZone Property on the SimpleDateFormat and then parsing your input String using it.

If I understand your problem correctly, you want the console to print the Date in IST and not in UTC or GMT. For this you just need to change the default TimeZone to IST by doing:

TimeZone.setDefault(TimeZone.getTimeZone("IST"));

Here is the code snippet:

public static void main (String[] args) throws Exception
{
    String startingDate = "25 Mar 2016";
    SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy");
    sdf.setTimeZone(TimeZone.getTimeZone("IST"));

    TimeZone.setDefault(TimeZone.getTimeZone("IST"));
    System.out.println(sdf.parse(startingDate));
}

Output:

Fri Mar 25 00:00:00 IST 2016
Sign up to request clarification or add additional context in comments.

1 Comment

@Rahul Your Welcome! Request you to accept the answer if it helped you.
3

I wanted the date output to be "25 Mar 2016"..

By default when you print Date instance toString method of Date class will be called and it will print the data of Date in EEE MMM dd HH:mm:ss zzz yyyy format and so that you are getting Thu Mar 24 18:30:00 UTC 2016.

Currently, if you already have Date in String you don't need to format date you can simply use that String. You can also use SimpleDateFormat#format(String dateString) method to format the Date according to the specific pattern, here dd MMM yyyy and so format method will return you the formatted String of Date instance.

NOTE : Parsing the Date does not format the value of Date object. SimpleDateFormat#parse will convert the String of dd MMM yyyy to Date instance while SimpleDateFormat#format will return the String from Date in dd MMM yyyy format.

SimpleDateFormat#setTimeZone says,

The TimeZone set by this method may be overwritten as a result of a call to the parse method.

Moreover, you can not set TimeZone on Date Object you use TimeZone.setDefault(TimeZone.getTimeZone("IST")); method before printing the Date which is already suggested by user2004685.

5 Comments

But why to convert the String to Date in the first place if the required output is String itself?
No problem if i get Time.. but i need that in IST format and the output is in UTC format because of which my date is "24th March" instead of "25th March"
@Rahul How are you getting output as Thu Mar 24 18:30:00 UTC 2016 ? By printing Date on console ?
Nopes. I have used Logger and checked values in log of my app(On Server).
Thanx for answering and explaining @TAsk.
2

The Question and other Answers are using outmoded classes.

Avoid old date-time classes

The old java.util.Date/.Calendar, SimpleDateFormat, and such bundled with the earliest versions of Java have proven to be poorly designed, confusing, and troublesome. Avoid them.

java.time

The java.time framework is built into Java 8 and later. Backports available for Java 6 & 7 and Android.

These new classes are a vast improvement over the old date-time classes. Search Stack Overflow for more discussions and examples.

For date-only values with no time-of-day, use the LocalDate class.

Specify a Locale for the human language to interpret the name-of-month. If omitted the JVM’s current default Locale is implicitly applied. Better to specify explicitly the desired/expected Locale. For example, Locale.US or Locale.ENGLISH or new Locale( "en" , "IN" ), whatever is appropriate to your incoming data.

String input = "25 Mar 2016";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "dd MMM yyyy" );
formatter = formatter.withLocale( Locale.ENGLISH );  // Specify the language to interpret name-of-month.
LocalDate localDate = LocalDate.parse( input , formatter );

To get a date-time with the first moment of the day, specify a time zone to create a ZonedDateTime.

ZoneId zoneId = ZoneId.of( "Asia/Kolkata" ); // Or "Europe/Paris", "America/Montreal", and such.
ZonedDateTime zdt = localDate.atStartOfDay( zoneId );

Never assume the day starts at the time 00:00:00.0. Anomalies such as Daylight Saving Time (DST) means the day may begin at another time. Let java.time handle such details for you by calling atStartOfDay.

1 Comment

Thanx for answering will try and use this too.
0

You can get it here:

String startingDate="25 Mar 2016";
DateFormat dateFormat = new SimpleDateFormat("dd MMM yyyy");
System.out.println("Date: " + dateFormat.parse(startingDate));

1 Comment

Thanx for answering but i got the answer from user2004685

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.