Actually in my java program like the following code...
String date1=null;
String formate="IST";
SimpleDateFormat sourceFormat = new SimpleDateFormat("z");
SimpleDateFormat gmtFormat = new SimpleDateFormat("'GMT('Z')'");
date1 = gmtFormat.format(sourceFormat.parse(formate));
System.out.println(date1);//output GMT(+0530)
Hear it is giving Correct Value but the timezone may change like PST---- GMT(-0800) will so.
But my code alway show only GMT(+0530)
Please help me to convert timezone ACT,,PST,IST.....etc to GMT(+11:00),GMT(-08:00),GMT(+0530).......etc
java.text.SimpleDateFormat sourceFormat = new SimpleDateFormat("z");
java.text.SimpleDateFormat gmtFormat = new SimpleDateFormat("'GMT('ZZZ')' zzzz");
java.util.Date date1 = sourceFormat.parse("IST");
TimeZone gmtTime = TimeZone.getTimeZone("IST");
gmtFormat.setTimeZone(gmtTime);
//System.out.println("Source date: " + date1);
System.out.println(" "+ gmtFormat.format(date1));
Way cleaner in my opinion.
TimeZone gmtTime = TimeZone.getTimeZone("IST");
long gmtOffset = gmtTime.getOffset(new Date().getTime())/ TimeUnit.HOURS.toMillis(1);
Related
I used two calender for starting date and finishing as below,
date.Ic.add(Restrictions.between("islemZamani", date1, date2));
However result of this criteria has results of date1 and between of them,not include results of date2.I mean ,It shows date1<= results. I want date1<= results<=date2. So I tried SimpleDateFormat like this;
String tar1 = new String();
String tar2 = new String();
TimeZone tz = TimeZone.getTimeZone("Europe/Istanbul");
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSZ");
dateFormat.setTimeZone(tz);
tar1 = dateFormat.format(date1);
tar2 = dateFormat.format(date2);
tar1 = tar1.substring(11, 18) + "000000";
tar2 = tar2.substring(11, 18) + "235959";
c.add(Restrictions.between("islemZamani", tar1, tar2));
Now It gives NullPointerException.How can i solve this problem? Do you suggest any different way from SimpleDateFormat? Thanka for any reply.
Use a string to manipulate date is too complicated and may case a loooot of problems.
Use only java.util.Date and/or java.util.GregorianCalendar.
Try this :
GregorianCalendar calendar1 = new GregorianCalendar();
calendar1.setTime(date1);
// Edit the calendar1 here if you want to
GregorianCalendar calendar2 = new GregorianCalendar();
calendar2.setTime(date2);
calendar2.add(GregorianCalendar.DAY_OF_YEAR, 1);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSZ");
TimeZone tz = TimeZone.getTimeZone("Europe/Istanbul");
dateFormat.setTimeZone(tz);
String tar1 = dateFormat.format(calendar1.getTime());
String tar2 = dateFormat.format(calendar2.getTime());
c.add(Restrictions.between("islemZamani", tar1, tar2));
But without the trace, I'm not sure this will solve your problem...
And, if it's possible, use the date instead of the string in your restriction.
I would like to get the date in numbers in Java.
For example: today is 13th of dec, 2012. I would like to get it in numbers as 13.12.2012
How can I achieve it? Any help would be really appreciated.
P.S I tried something like:
Date d = new Date();
String date = d.toString().substring(0, 10);
But got the output as:Thu Dec 13
DateFormat df = new SimpleDateFormat("dd.MM.yyyy");
String formattedDate = df.format(new Date());
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
String res = format(d);
You can use SimpleDateFormat to achieve that
new java.text.SimpleDateFormat("dd.MM.yyyy").format(new Date());
DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy' 'HH:mm:ss");
Date d = (Date)formatter.parse(dateTime);
System.out.println("date in controller "+d);
I get the output as
date in controller Mon Dec 31 16:04:57 IST 2012
Please suggest a method to output the date in MM/dd/yyyy HH:mm:ss format.
Need the output in date format and not as string so as to store the output in datetime field in mysql db
Need the output in date format and not as string so as to store the output in datetime field in mysql db
After the statement
Date d = (Date)formatter.parse(dateTime);
java.sql.Date sqldate = new java.sql.Date(d.getTime())
you have got a java.util.Date object and you can store it as it is in mysql DB (column type : datetime).
However, when you are printing d, it defaults to the .toString() implementation. I think you expected some output like Date# but the toString printed in user readable format thats why the confusion.
You are using d an object of Date class, so its toString method is called that gives the output as Mon Dec 31 16:04:57 IST 2012.
If you want to display it in the format that you have specified in your SimpleDateFormat then try using this :
DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy' 'HH:mm:ss");
Date d = (Date)formatter.parse(dateTime);
System.out.println("date in controller "+ formatter.format(d));
Don't see why the single-qoutes (') are used in the format-string and you also need to catch ParseException:
DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Date d = new Date();
try {
d = (Date)formatter.parse("12/31/2012 12:13:14");
} catch(ParseException pex) { System.out.println(pex.getMessage()); }
// convert the date into java.sql.Date
java.sql.Date sqldate = new java.sql.Date(d.getTime());
// then put it in the database, something like this:
//resultSet.updateDate("myDateTimeField", sqldate);
SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy' 'HH:mm:ss");
System.out.println("date in controller"+ df.format(d));
Try using format instead of parse.
Date date = new Date();
String DATE_FORMAT = "MM/dd/yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
System.out.println("Today is " + sdf.format(date) );
How would I convert a String date in the form of 24/04/2012 to a date variable in the format of 24-Apr-12, which can later be passed into my Oracle database.
I have tried this but it says the string date is unparsable:
DateFormat format = new SimpleDateFormat("dd-MMM-yy");
Date newDate = (Date) format.parse(date);
I think you are confusing parsing and formatting, try this:
String old_date = "24/04/2012";
DateFormat old_format = new SimpleDateFormat("dd/MM/yyyy");
Date newDate = (Date) old_format.parse(old_date);
DateFormat new_format = new SimpleDateFormat("dd-MMM-yy");
String date = new_format.format(newDate);
System.out.println(date);
You are doing this backwards:
DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
Date newDate = format.parse(date);
DateFormat formatOutput = new SimpleDateFormat("dd-MMM-yyyy");
String output = formatOutput.format(newDate);
But if what you are doing is passing the date to Oracle, you should really use a PreparedStatement
Your date format should be "dd/MM/yyyy"
SimpleDateFormat sdf= new SimpleDateFormat("dd-MM-yy");
DateTime newDate = sdf.format(date);
Try DateFormat format = new SimpleDateFormat("dd/MM/yyyy")
Why don't you try using java.sql.Date.
For example:
Date newDate = new java.sql.Date(date.getTime());
So you can just give an generic sql object for date.
The Date class of Java is quite difficult to use in many cases. I recommend the use of the much better Joda Time classes:
DateTimeFormatter oldFormat = DateTimeFormat.forPattern("dd/mm/yyyy");
DateTimeFormatter newFormat = DateTimeFormat.forPattern("dd-MMM-yy");
DateTime dateTime = oldFormat.parseDateTime("24/04/2012");
String newDate = newFormat.print(dateTime);
I was trying to format a string into date.
For this I have written a code:-
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
System.out.println(sdf.format( cal.getTime() ));
This is fine..
But now I want to convert a string into a date formatted like above..
For example
String dt="2010-10-22";
And the output should be like this:-
2010-10-22T00:00:00
How do I do this?
String dt = "2010-10-22";
SimpleDateFormat sdfIn = new SimpleDateFormat("yyyy-MM-dd");
ParsePosition ps = new ParsePosition(0)
Date date = sdfIn.parse(dt, pos)
SimpleDateFormat sdfOut = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
System.out.println(sdfOut.format( date ));
This should do it for you, remember to wrap it in a try-catch block just in case.
DateFormat dt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
try
{
Date today = dt.parse("2010-10-22T00:00:00");
System.out.println("Your Date = " + dt.format(today));
} catch (ParseException e)
{
//This parse operation may not be successful, in which case you should handle the ParseException that gets thrown.
//Black Magic Goes Here
}
If your input is going to be ISO, you could also look at using the Joda Time API, like so:
LocalDateTime localDateTime = new LocalDateTime("2010-10-22");
System.out.println("Formatted time: " + localDateTime.toString());
The same class you use for output formatting of dates can also be used to parse dates on input.
SimpleDateFormat reference
To use your example, to parse the sample date:
String dt = "2010-10-22";
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(dateFormatter.parse(dt));
The fields that are not specified (ie. hour, minutes, etc) will be 0. So your same code can be used to format the date on output.
Date Format Example
Containing the Conversion of String Date object from one format to another