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.
Related
I'm trying to get the current date/time add a number of days and display in a UTC format.
Current output is like this: 2019-05-09T11:11:4226
See code for below, I think I'm maybe taking the wrong approach to this.
int NumDaysInFurure = 1;
Date currentDate = new Date();// get the current date
SimpleDateFormat daateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
daateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
String date = ""+dateFormat.format(currentDate) + NumDaysInFurure;//add one day to the current date
Log.i("the future date is", date);
Starting from Java8, this would be a better approach:
DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss").format(LocalDateTime.now().plusDays(1))
to break it down:
LocalDateTime.now()
returns an instance of LocalDateTime with the current date and time.
You add one day (using the plusDays(Long days)) method.
The result of this, you format using a DateTimeFromatter.
The result of the complete is a String with the right date/format.
I'm trying to update the last modified date of a specific folder, here's what I've got:
public void touchFolder(){
File folderToTest = new File("C:\\Temp");
SimpleDateFormat dateFormatUtc = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dateFormatUtc.setTimeZone(TimeZone.getTimeZone("UTC"));
String newTime = dateFormatUtc.format(new Date());
folderToTest.setLastModified(Long.parseLong(newTime));
}
I am just putting this code in a test case so don't worry about calling this method etc.
I'm getting errors with the parsing that date format as a long, what's the format used in setting the last modified date & time?
This is an example from the documentation, using java.nio.file.Files:
Path path = ...
FileTime now = FileTime.fromMillis(System.currentTimeMillis());
Files.setLastModifiedTime(path, now);
I think you should just do folderToTest.setLastModified(System.currentTimeMillis());
In your code newTime is a formatted date 2018-12-19 15:21:31 which can't be parsed to Long. What you want to do is supply the time in milliseconds e.g.:
Date d = new Date();
file.setLastModified(d.getTime());
As per File.setLastModified() method javadoc:
time - The new last-modified time, measured in milliseconds since the epoch (00:00:00 GMT, January 1, 1970)
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 have Date today=new Date(); which returns the current date.. but when i try to display date,month,year separately with the help of
DateFormat mmFormat=new SimpleDateFormat("MM");
System.out.println(mmFormat.format(today.getMonth()));
DateFormat yyFormat=new SimpleDateFormat("yyyy");
System.out.println(yyFormat.format(today.getYear()));
it prints month as 01 and year as 1970
how to resolve this.?
mmFormat.format(today.getMonth())
You're passing an integer – the month of the date – to a date format method.
The format method interprets that integer as a UNIX timestamp – a number of seconds since 1970.
You need to pass the date itself to the formatter.
Pass the entire date to SimpleDateFormat. The format string "MM" or "yyyy" will cause it to just extract the part of the date you want.
Just use the Date today as the input argument
System.out.println(mmFormat.format(today));
and
System.out.println(yyFormat.format(today));
today.getMonth() and today.getYear() returns an int which is interpreted as an UNIX timestamp . The value is 1 and 113 , which corresponds to approximately January 1, 1970, 00:00:01 GMT and January 1, 1970, 00:01:53 GMT represented by this Date object. To get the desired result , you need to pass the Date object :
System.out.println(mmFormat.format(today));
You would need to use Calendar. Have a look at the java docs.
You can do it like this -
Calendar cal = Calendar.getInstance();
System.out.println(cal.get(Calendar.DAY_OF_MONTH));
System.out.println(cal.get(Calendar.MONTH)); // month in the Calendar class begins from 0
System.out.println(cal.get(Calendar.YEAR));
System.out.println(cal.get(Calendar.HOUR_OF_DAY));
System.out.println(cal.get(Calendar.MINUTE));
System.out.println(cal.get(Calendar.SECOND));
This would help you to avoid creating multiple DateFormat objects. Also in case you want to use another date instead of today's date the you can just pass the date to the cal.setTime() method.
That is because all these methods are deprecated. Use
Calendar myCalendar = GregorianCalendar.getInstance();
myCalendar.get(Calendar.MONTH);
myCalendar.get(Calendar.YEAR);
myCalendar.get(Calendar.DAY_OF_MONTH);
Better in this way
Date date=new Date(); // your date
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);
System.out.println(year+"\n"+month);
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.