0

this is code , the output of the code is "Thu Jun 06 08:00:00 PKT 2013" , but i want the format 2013-06-08 00:00:00 , kindly help me

 import java.text.SimpleDateFormat; import java.text.ParseException; import java.util.*; import javax.script.*; public class time { public static void main (String[] args)throws ParseException{ String date1 = "2013/06/06"; String time1 = "08:00 AM"; String time2 = "18:00 PM"; SimpleDateFormat sdf = new SimpleDateFormat("yyy/MM/dd hh:mm "); try{ Date dateObj1 = sdf.parse(date1 + " " + time1); Date dateObj3 = sdf.parse(date1 + " " + time2); System.out.println("Date Start: "+dateObj1); System.out.println("Date End: "+dateObj3); int c=0; long dif = dateObj1.getTime(); while (dif < dateObj3.getTime()) { System.out.println(c++); Date slot = new Date(dif); System.out.println("Hour Slot --->" + slot); dif+=3600000; } System.out.println("c is :"+c); } catch(ParseException e){ ; } } } 
1
  • 1
    By convention, Java type names usually start with an uppercase letter. Commented Jun 9, 2013 at 8:40

2 Answers 2

0

The format of a Date is only for display purpose. We shouldn't be concerned how the run time represents or stores the Date object internally . See the comments in code :

String date1 = "2013/06/06"; String time1 = "08:00 AM"; // You are trying to convert the String "2013/06/06 08:00 AM" to a Date object // for this you tell the DateFormat that the date string is formatted as // "yyyy/MM/dd hh:mm a" , so when you parse the String to Date , it is converted to // a valid and expected Date object. DateFormat df = new SimpleDateFormat("yyyy/MM/dd hh:mm a"); Date date = df.parse(date1+" "+time1); System.out.println(date); //Thu Jun 06 08:00:00 IST 2013 // To print "2013-06-06 08:00:00" // You tell SimpleDateFormat to format the date as ""yyyy-MM-dd hh:mm:ss" while //printing System.out.println(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(date)); 
Sign up to request clarification or add additional context in comments.

2 Comments

What is not working ?
this format , when i write it in constructor
0

Use this

 String date1 = "2013/06/06"; String time1 = "08:00 AM"; String time2 = "18:00 PM"; SimpleDateFormat sdf = new SimpleDateFormat("yyy/MM/dd hh:mm "); SimpleDateFormat sdf1 = new SimpleDateFormat("yyy-MM-dd hh:mm:ss "); try{ Date dateObj1 = sdf.parse(date1 + " " + time1); Date dateObj3 = sdf.parse(date1 + " " + time2); System.out.println("Date Start: "+sdf1.format(dateObj1)); System.out.println("Date End: "+sdf1.format(dateObj3)); int c=0; long dif = dateObj1.getTime(); while (dif < dateObj3.getTime()) { System.out.println(c++); Date slot = new Date(dif); System.out.println("Hour Slot --->" + sdf1.format(slot)); dif+=3600000; } System.out.println("c is :"+c); } catch(ParseException e){ ; } 

My advice: Java 6 's data conversions is bad. Use joda time

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.