I am having trouble formatting date in java.
I have a shopping class that has item name in string, quantity in int and a date in Date.
In the run class I am trying to run this query.
Date date = new Date(202020);
SimpleDateFormat dateformat = new SimpleDateFormat("dd-mm-yy");
String date1 = dateformat.format(date);
Shopping shoplist1 = new Shopping ( "iphone", 2, date);
When I try to create a new shopping entery with the date the date does not format. I don't know why but it gives me Thu Jan 01 01:03:22 GMT 1970.
You are using wrong Date class constructor:
Date date = new Date(202020);
Means that you are trying to allocate a Date object and initialize it to represent the specified number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT.
You should use something like this:
Shopping shoplist1 = new Shopping ("iphone", 2,
new SimpleDateFormat("dd-MM-yy").parse("20-20-20"));
Read documentation to get more difference between DateFormat#parse(...) and DateFormat#format(...)
From your comment :
"I tried to add date 1, but it wouldn't allow it. It says string can not be converted to date"
You are passing string to your constructor but looks like it can take java.util.Date object
Shopping shoplist1 = new Shopping ( "iphone", 2, date);
Change your constructor signature to take a String object
OR
Pass the date object and perform the formatting in any other relevant location
change SimpleDateFormat dateformat = new SimpleDateFormat("dd-mm-yy"); to SimpleDateFormat dateformat = new SimpleDateFormat("dd-MM-yyyy");
if I clearly remember small mm mean minutes not month
Read this post about date formatting
and also as say #zvzdhk, you incorrectly use date constuctor.
You create the date instance from a timestamps (milliseconds from 1970). I suppose that is not what you are trying to do.
You format the date into a string and pass the date. If you want to use the formatted date, you have to pass the string.
Related
I have this code:
Date dt = new Date(100000000000L);
DateFormat[] dtformat = new DateFormat[6];
dtformat[0] = DateFormat.getInstance();
dtformat[1] = DateFormat.getDateInstance();
dtformat[2] = DateFormat.getDateInstance(DateFormat.MEDIUM);
dtformat[3] = DateFormat.getDateInstance(DateFormat.FULL);
dtformat[4] = DateFormat.getDateInstance(DateFormat.LONG);
dtformat[5] = DateFormat.getDateInstance(DateFormat.SHORT);
for(DateFormat dateform : dtformat)
System.out.println(dateform.format(dt));
I don't know what is the meaning of this code (the 100000000000L) function:
Date dt = new Date(100000000000L);
DateFormat[] dtformat = new DateFormat[6];
Can someone tell me? Because I want to replace the time to the 24 July 1998
The docs clearly state that the parameter for constructing a Date is:
milliseconds since January 1, 1970, 00:00:00 GMT not to exceed the milliseconds representation for the year 8099. A negative number indicates the number of milliseconds before January 1, 1970, 00:00:00 GMT.
So, 0 would represent Midnight 1 Jan, 1970.
However, if I were you, I would stop using Date and use LocalDate which is much easier to use.
Date(long date)
Its a parameterized constructor of Date Class in java which Allocates a Date object and initializes it to represent the specified number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT.
For more information you can refer to below link
Date Class in Java
john16384 in his answer says
if I were you, I would stop using Date and use LocalDate, which is
much easier to use.
I agree. Here’s how:
LocalDate dt = LocalDate.of(1998, Month.JULY, 24);
DateTimeFormatter[] dtformat = {
DateTimeFormatter.BASIC_ISO_DATE,
DateTimeFormatter.ISO_LOCAL_DATE,
DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL),
DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG),
DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM),
DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT)
};
for (DateTimeFormatter dateform : dtformat) {
System.out.println(dt.format(dateform));
}
Note how straightforward it is to create the desired date (to do the same with an old-fashioned Date object, you would be required to go through a class named Calendar). I am using an array initializer instead of explicitly assigning an object to each element of the array. It’s simpler.
On my computer (Danish locale) the above code prints:
19980724
1998-07-24
24. juli 1998
24. juli 1998
24-07-1998
24-07-98
If you want to control the locale explicitly (sometimes a good idea to avoid surprises), you may use DateTimeFormatter.withLocale(), for example DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG).withLocale(new Locale("Indonesian")) or DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG).withLocale(Locale.ROOT).
I have unix timestammp stored in mysql. I am converting it into time. It displays wrong time.
Here is code:
Date date = new Date((long)timestamp*1000);
SimpleDateFormat sdf = new SimpleDateFormat("h:mm a");
sdf.setTimeZone(TimeZone.getTimeZone("GMT+5:30"));
timeString = sdf.format(date);
System.out.println(timeString);`
timestamp is variable which contains unix timestamp.
Ex: for timestamp=1417437428505 it should show 6:07 PM and showing 12:31 AM
What is solution for it?
You're multiplying a timestamp which is already in milliseconds since the Unix epoch by 1000. You just want:
Date date = new Date(timestamp);
If you look at all of the date, not just the time, you'll see it's currently in 46886!
Are You sure that need multiplied by 1000? I tried pass without multiplying Date date = new Date(timestamp); and it printed 6:07 PM
Remove multiply with 1000 in
Date date = new Date((long)timestamp*1000);
than it works.
I want to set a hand written String as the date for a Date object. What I'm trying to say is that I want to do is this:
String date= [date string here!!!];
Date mydate = new Date(date);
Something like that. The reason I want to do this is because I want my network to have standard Date and Time because since I run them from the same machine the time is being taken from the same clock and it gets different time every time. So I want to get that time and also add 1-2 seconds in the end so I can test my nodes with different times.
Java is strongly typed language. You cannot assign string to Date. However you can (and should) parse string into date. For example you can use SimpleDateFormat class like the following:
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
Date date = fmt.parse("2013-05-06");
you'll want to use dateformatter
DateFormat formatter = new SimpleDateFormat("MM/dd/yy");
Date date = formatter.parse("01/29/02");
String string = "January 2, 2010";
Date date = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH).parse(string);
System.out.println(date); // Sat Jan 02 00:00:00 BOT 2010
updated
String string ="2013-04-26 08:34:55.705"
Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").parse(string);
System.out.println(date);
I create a Android app and I want to show the current date on screen. I use this code:
Date date = new Date(0);
java.text.DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
text.setText("Time: " + dateFormat.format(date));`
But this code shown me date "1.1.1970".
How can I show the current date?
Date(0) is the first January 1970, see the documentation of the Date(long milliseconds) constructor:
public Date(long milliseconds)
Initializes this Date instance using the specified millisecond value. The value is the number of milliseconds since Jan. 1, 1970 GMT.
To get the current date, use Date(), the constructor without a parameter:
public Date()
Initializes this Date instance to the current time.
use
text.setText(new SimpleDateFormat("yyyy-MM-dd").format( Calendar.getInstance().getTime());
SimpleDateFormat dfDate = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
String date = dfDate.format(c.getTime());
System.out.println("Date is "+date);
Result will be ========> 2012-08-07
Just remove the 0, so replace this: Date date = new Date(0); by this: Date date = new Date();
As per Android Documentation:
Initializes this Date instance using the specified millisecond value.
The value is the number of milliseconds since Jan. 1, 1970 GMT.
Since you passed on a value of 0, it was taking the above date and adding 0 milliseconds to it, hence the reason why you got the 1.1.1970 date.
To show the current Date on the Screen do this..
String s = new SimpleDateFormat("dd-MM-YYYY").format(new Date());
Now display the String s on the screen.
new Date() is enough otherwise you get the date 0 seconds after the 01.01.1970 00:00:00 if you call new Date(0)
Don't use new Date(0). The parameter is time since the epoch, which is 1.1.1970. Just use new Date().
Make sure you import "java.util.Date;" and not "java.sql.Date;" or else you wont be able to pass through no parameter.
I have to get a Date in type Date, not in String.
I have this code:
DateFormat formatter = new SimpleDateFormat("MM/dd/yy");
Date date1 = new Date();
String date = (formatter.format(date1));
// At this point I get the date in correct format i.e 05/24/11
Date todaysDate = (Date)formatter.parse(date);
// But after this I get the date in format : Tue May 24 00:00:00 EDT 2011
// whereas I Want to get the date like above i.e 05/24/11
// And in type Date, not in type String
If anyone could help, thanks
The Date object just represents a point in time and has no notion of a format (or time zone). If you print out a Date object it first converts it to a String using the default formatting of EEE MMM dd HH:mm:ss zzz yyyy. If you want a specific formatting when you print it or otherwise represent it as a String, you'll need to use a formatter just like you already have.
In other words, you want Date.toString() to return the same as DateFormat.format()? You could just do exactly that:
public class MyDate extends Date {
DateFormat formatter = new SimpleDateFormat("MM/dd/yy");
public String toString() {
return this.formatter.format(this);
}
}
But do you really want to mix up presentation (date format) with your data?
There is no problem here, you have a Date representing and can save it into the DB as it is now. If you print it to the console it gets formatted according the default rules, this is why you think it is different from what you need, but it has actually already the right value.
So just go ahead and put it into your DB.
Chances are that you DB will hold on getting a Timestamp, in this case you can create one:
Date d = ...
java.sql.Timestamp ts = new java.sql.Timestamp(d.getTime());
and save this one.