Date showing is wrong - java

I use this method to generate my today date to write it to file.
public void DateFiledControl() {
Calendar currentTime = Calendar.getInstance();
SimpleDateFormat myFormat = new SimpleDateFormat("YYYY/MM/DD");
String strDate = myFormat.format(currentTime.getTime());
System.out.println(strDate);
}
But , my result is : 2013/03/63
Why?!

Because you should be using dd instead of DD, which the SimpleDateFormat documentation makes fairly clear.

Also, be aware that 'MM' is the month, but 'mm' is the minutes.
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

try
SimpleDateFormat myFormat = new SimpleDateFormat("yyyy/MM/dd");

Related

Java - SimpleDateFormat parse from String issue

I'm trying to parse the following string to a Date object:
String str = "04/15/2014 10:30:24"
I'm using SimpleDateFormat :
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
java.util.Date orderDate = sdf.parse(str);
java.sql.Date orderSqlDate = new java.sql.Date(orderDate.getTime());
but orderSqlDate always returned: 04/15/2014 00:00:00
how to use SimpleDateFormat in java exactly?
The java.sql.Date javadoc states
To conform with the definition of SQL DATE, the millisecond values
wrapped by a java.sql.Date instance must be 'normalized' by setting
the hours, minutes, seconds, and milliseconds to zero in the
particular time zone with which the instance is associated.
If you're going to use java.sql.Date, there's no way around this.
You are also doing correct.
But to get the result in the format you want, you need to use .format("/your format/") method after parsing the string.
String date = "15/12/2014 10:42:24";
SimpleDateFormat dateParser = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date parseDate = dateParser.parse(date);
formatter.format(parseDate) // this will change format of date as you want.
I don't think the way you parse is wrong. Are you sure you print orderDate right ?
The following code demonstrates both parsing and formatting (printing).
public static void main(String[] args) {
String format = "MM/dd/yyyy HH:mm:ss";
try {
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date orderDate = new SimpleDateFormat(format).parse("04/15/2014 10:30:24");
System.out.println(sdf.format(orderDate));
} catch (Exception e) {
e.printStackTrace();
}
}
Provide Locale in the SimpleDateFormat constructor, otherwise parsing might be dependant on your local settings:
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss", Locale.ROOT);

convert a date object to Oracle timestamp type string

I want to convert a date object, ex: new Date(), to a string which has a format like Oracle's time stamp type, ex: 21-OCT-13 11.08.13.858000000 AM. I know I could just get each piece of information in the date object like day, month, year, hour, minute, ... to form the Oracle format string but I really want to know is there a utility to do that instead?
Using SimpleDateFormat#format() you would print a Date as
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yy hh.mm.ss.SSSSSSSSS a");
System.out.println(sdf.format(new Date()).toUpperCase());
Output :
21-OCT-13 10.01.38.000000614 AM
See JavaDocs for Date and Time patterns.
Try taking a look at SimpleDateFormats - That would be your best bet and easiest way of doing it.
Eg:
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm:ss"); //Hours:Minutes:Seconds
String strDate = dateFormat.format(date);
Use SimpleDateFormat.
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("your_format_here"); // dd/MM/yy h:mm:ss a
String formattedDate = sdf.format(date);
System.out.println(formattedDate);

trouble with local time

i use these codes to get my local time:
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar currenDdate = Calendar.getInstance(TimeZone.getDefault());
System.out.print("Last update: "+dateFormat.format(currenDdate.getTime()));
or:
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar currenDdate = Calendar.getInstance(getTimeZone("GMT+3.5"));
System.out.print("Last update: "+dateFormat.format(currenDdate.getTime()));
but non of them give the local time, they are both giving me GMT
what is the problem?
NOTE: I am using eclipse and android programming, the codes above correctly work in java but in android it is not!
instead of system.out.print() i use a textview to show the time
TextView date = (TextView) findViewById(R.id.gold_textView1);
date.setText("Last update: "+dateFormat.format(currenDdate.getTime()));
please help me find the problem
Try Date currentDate = new Date() instead of the Calendar object. Your System.out.print() call won't change. Also, double check your system to make sure its default timezone is set correctly.
Have you tried like this:
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
dateFormat.setTimeZone(TimeZone.getDefault());
Calendar currenDdate = Calendar.getInstance(TimeZone.getDefault());
System.out
.print("Last update: " + dateFormat.format(currenDdate.getTime()));
Instead of Calendar you need to set your TimeZone on SimpleDateFormat:
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss Z");
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT+0330"));
// prints: Last update: 2013/07/31 18:46:24 +0330
System.out.println("Last update: " + dateFormat2.format(currenDdate.getTime()));
This is required because SimpleDateFormat uses a Calendar instance of its own internally.
See SimpleDateFormat#setTimeZone()
public void setTimeZone(TimeZone zone)
{
calendar.setTimeZone(zone);
}
Notice, the date format pattern also includes the time zone letter Z to include it in the display.
Try this, use JodaTime http://www.joda.org/joda-time/
DateTimeZone dateTimeZoneUTC = DateTimeZone.UTC;
DateTime dateTimeUTC = new DateTime().withZone(dateTimeZoneUTC);
DateTimeZone dateTimeZoneLocal = DateTimeZone.getDefault();
DateTime dateTimeLocal = dateTimeUTC.withZone(dateTimeZoneLocal);
DateTimeZone dateTimeZoneOffset = DateTimeZone.forID("-03:00");
DateTime dateTimeOffset = dateTimeLocal.withZone(dateTimeZoneOffset);
// Airport Current Time hh/mm
DateTimeFormatter timeFormat = DateTimeFormat.forPattern("hh/mm").withZone(dateTimeZoneOffset);
currentTime.setText(timeFormat.print(dateTimeOffset));

SimpleDateFormat doesn't work

I'm trying to do something with a SimpleDateFormat, but after I read the Javadoc, I only got more confused. I want two methods, one for the timezone and one for the current time and date. My format should look like this:
Time Zone: GMT +01:00
Time and Date: Wednesday 17/04/2013, 20:38:34
I took code from the internet once for time. That worked fine:
private String getFormattedTime() {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
return sdf.format(cal.getTime());
}
This will output: 20:41:34
Now for my other format, I tried something like this (I wasn't completely done yet):
private static String getFormattedDate() {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("EEEEEEEEE DD/MM/yyyy, HH:mm:ss");
return sdf.format(cal);
}
private static String getTimeZone() {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("z");
return sdf.format(cal.getTimeZone());
}
If you'd run this code, you get an IllegalArgumentException at the first return line.
It seems like the Javadocs don't give any example on how to use this.
SimpleDateFormat acts on Dates, not Calendars. To convert, use .getTime(), so your code should read:
private static String getFormattedDate() {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("EEEEEEEEE dd:MM:yyyy, HH:mm:ss");
return sdf.format(cal.getTime());
}
private static String getTimeZone() {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("z");
return sdf.format(cal.getTime());
}
However, using joda is probably a better way to go for reasons I could write a PhD thesis on.

Convert a string to a GregorianCalendar

How do a I take an input birthday string such as 02 26 1991 and make it into a Gregorian Calendar?
I tried parsing it first but it keeps giving me an error message so I'm not quite sure what I'm doing wrong. I also have other input data before this date. One is another string and one is a double value.
Use SimpleDateFormat to parse the date and then assign it to a Calendar.
DateFormat df = new SimpleDateFormat("dd MM yyyy");
Date date = df.parse("02 26 1991");
Calendar cal = Calendar.getInstance();
cal.setTime(date);
The third line could be replaced with:
Calendar cal = new GregorianCalendar();
but I prefer the first version.
Use a DateFormat as shown here:
Example:
DateFormat dateFormat = new SimpleDateFormat("hh:mm dd/MM/yy");
dateFormat.setLenient(false);
Date d = dateFormat.parse("06:23 01/05/06");
Use the parse() method of the SimpleDateFormat class. You can use setLenient(false) to force strict parsing.

Categories

Resources