This question already has answers here:
How to get current time and date in Android
(42 answers)
Closed 8 years ago.
I am using this Java method here to get the current time:
final Date d = new Date();
d.getTime();
1390283202624
What I am getting a numeric figure of datatype long. What I need is the exact time in the format hh:mm:ss. And in the end I also have to perform arithmetic on the figure obtained.
Any clue? Also is this a reliable way of obtaining time on Android phone because I am getting a constant value here?
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
String formattedDate = sdf.format(d);
System.out.println(formattedDate);
Calendar instance = Calendar.getInstance();
int hour = instance.get(Calendar.HOUR);
int minute = instance.get(Calendar.MINUTE);
int second = instance.get(Calendar.SECOND);
use:
SimpleDateFormat sdf=new SimpleDateFormat("hh:mm:ss");
String time=sdf.format(new Date());
Use Calendar class.
Calendar c = Calendar.getInstance();
int seconds = c.get(Calendar.SECOND);
See this question. Calendar class contain all desired information.
Related
This question already has answers here:
Get integer value of the current year in Java
(16 answers)
Closed 5 years ago.
I need a Java program that subtracts 5 years from the current year.
Everything is working fine but after I run the program:
DateFormat dateFormat = new SimpleDateFormat("yyyy");
Calendar cal = Calendar.getInstance();
cal.add(Calendar.YEAR,-5);
Date today = new Date();
String start = dateFormat.format(cal.getTime()).toString();
String end = dateFormat.format(today).toString();
double start_doub = Double.parseDouble(start);
double end_doub = Double.parseDouble(end);
System.out.println(start_doub);
System.out.println(end_doub);
The result is:
2012.0
2017.0
I don't know the reason why the program adds .0 after the year?
How can I remove the last part?
Your code look like below
DateFormat dateFormat = new SimpleDateFormat("yyyy");
Calendar cal = Calendar.getInstance();
cal.add(Calendar.YEAR,-5);
Date today = new Date();
String start = dateFormat.format(cal.getTime()).toString();
String end = dateFormat.format(today).toString();
int start_doub = Integer.parseInt(start);
int end_doub = Integer.parseInt(end);
System.out.println(start_doub);
System.out.println(end_doub);
This question already has answers here:
Java: Date from unix timestamp
(11 answers)
Closed 6 years ago.
I am trying to get dd.MM.yyyy and hh:mm from 1436536800 but only the time is correct, the date is completely wrong. I don't really understand how this is supposed to work
int dt = time.getInt("start")*1000;
Date date = new Date(dt);
startDate = dateFormat.format(date);
If time.getInt("start") is a valid unix timestamp, you must add "000" to the number. Example: 1436536800 * 1000 = 1436536800000. Then you can use the timestamp to get a Date:
final Date date = new Date(Long.parseLong("1436536800000"));
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy hh:mm");
System.out.println(sdf.format(date));
Console exit: 10.07.2015 09:00
Assuming the time is correct, it's likely the fact that you're multiplying by 1,000. When creating the date the way you are, it takes in milliseconds. Is it possible that your input is already in milliseconds? (Your current method will be ~2 minutes off if so)
Date date=new Date(1436536800);
SimpleDateFormat df2 = new SimpleDateFormat("dd.MM.yyyy");
String dateText = df2.format(date);
Date you are getting is a JSON string value. follow steps below to format it correctly.
First download Moment.js file and add it in your project.
var date1 = "1436536800"; // your long value contain in this variable.
var date2 = moment(date1).format(MMMM Do YYYY);//It will give you formatted date value.
see more formats below
This question already has answers here:
Difference in days between two dates in Java?
(19 answers)
Closed 8 years ago.
i am trying to get the difference between two dates. one of the dates was parsed from a string(dateEmployd) and the other date is the current date (currentDate). This is what i did to get the dates...
public static Date getActiveService(String DtEmplydString){
SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");//formater for parsed String date
Date dateEmployd, currentDate,periodDifference = null;
try{
dateEmployd = ft.parse(DtEmplydString);
currentDate = new Date();
}catch(ParseException e){
JOptionPane.showMessageDialog(null, e);
}
return periodDifference;
}
Now, i am meant to return periodDifference but i dont know how i would find the difference betweent the two dates (dateEmployd and currentDate) and display it in years or days or a combination of both.
please guys much help is needed. thanks in advance...
Take a look at the jodatime library. They have functions like
DateTime dateEmployd = DateTimeFormat.forPattern("yyyy-MM-dd").parse(DtEmplydString);
Years.yearsBetween(dateEmployd, DateTime.now())
The same for Days.daysbetween, Seconds, Hours etc.
long diff = date2.getTime() - date1.getTime();
You could try out the Java.Calendar for date functions because Date is deprecated .
Here is an example with Cdate comparators
Calendar start = Calendar.getInstance();
start.setTime(from ( your initial Date object here ) );
Calendar end = Calendar.getInstance();
end.setTime(new Date());
int actualDays = start.compareTo(end);
This question already has answers here:
converting date time to 24 hour format
(12 answers)
Closed 8 years ago.
In an Android app I have an internal logging method and I add a time-date stamp to the start of each message . . .
public void logEvent(String sMsg) {
String delegate = "MM/dd/yy hh:mm:ss";
java.util.Date noteTS = Calendar.getInstance().getTime();
String sTod = " " + DateFormat.format(delegate,noteTS);
sMsg = sTod + " " + sMsg;
logEvents.add(sMsg);
. . .
This produces a time-date stamp that looks like "07/25/14 02:58:18". But I want the time to be in 24 hour format, i.e., "14:58:18".
In some systems that's accomplished by using "HH" instead of "hh" so I tried that, i.e.,
String delegate = "MM/dd/yy HH:mm:ss";
... but that just gave me "07/25/14 HH:58:18"
So what do I have to do to get the hours to be in 24-hour format?
Use SimpleDateFormat instead of DateFormat. Refer SimpleDateFormat Javadoc API: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
H Hour in day (0-23) Number 0
"MM/dd/yy HH:mm:ss" should work with SimpleDateFormat.
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yy HH:mm:ss");
java.util.Date noteTS = Calendar.getInstance().getTime();
String sTod = formatter.format(noteTS);
Try this:
private final SimpleDateFormat sdfTime = new SimpleDateFormat("kk:mm");
...
TEXTVIEW.setTitle(sdfTime.format(new Date()));
http://developer.android.com/reference/java/text/SimpleDateFormat.html
You can use very simple class for it.
Try this:
Time time=new Time();
time.setToNow();
textview.setText(time.hour+":"+time.minute);
Hope be useful for you.:)
Let's say I have this:
PrintStream out = System.out;
Scanner in = new Scanner(System.in);
out.print("Enter a number ... ");
int n = in.nextInt();
I have a random date, for example, 05/06/2015 (it is not a fixed date, it is random every time). If I want to take the 'year' of the this date, and add whatever 'n' is to this year, how do i do that?
None of the methods in the Date Class are 'int'.
And to add years from an int, 'years' has to be an int as well.
You need to convert the Date to a Calendar.
Calendar c = Calendar.getInstance();
c.setTime(randomDate);
c.add(Calendar.YEAR, n);
newDate = c.getTime();
You can manipulate the Year (or other fields) as a Calendar, then convert it back to a Date.
This question has long deserved a modern answer. And even more so after Add 10 years to current date in Java 8 has been deemed a duplicate of this question.
The other answers were fine answers in 2012. The years have moved on, today I believe that no one should use the now outdated classes Calendar and Date, not to mention SimpleDateFormat. The modern Java date and time API is so much nicer to work with.
Using the example from that duplicate question, first we need
private static final DateTimeFormatter formatter
= DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss");
With this we can do:
String currentDateString = "2017-09-12 00:00:00";
LocalDateTime dateTime = LocalDateTime.parse(currentDateString, formatter);
dateTime = dateTime.plusYears(10);
String tenYearsAfterString = dateTime.format(formatter);
System.out.println(tenYearsAfterString);
This prints:
2027-09-12 00:00:00
If you don’t need the time of day, I recommend the LocalDate class instead of LocalDateTime since it is exactly a date without time of day.
LocalDate date = dateTime.toLocalDate();
date = date.plusYears(10);
The result is a date of 2027-09-12.
Question: where can I learn to use the modern API?
You may start with the Oracle tutorial. There’s much more material on the net, go search.
Another package for doing this exists in org.apache.commons.lang3.time, DateUtils.
Date date = new Date();
date = DateUtils.addYears(date, int quantity = 1);
The Date class will not help you, but the Calendar class can:
Calendar cal = Calendar.getInstance();
Date f;
...
cal.setTime(f);
cal.add(Calendar.YEAR, n); // Where n is int
f = cal.getTime();
Notice that you still have to assign a value to the f variable. I frequently use SimpleDateFormat to convert strings to dates.
Hope this helps you.
Try java.util.Calendar type.
Calendar cal=Calendar.getInstance();
cal.setTime(yourDate.getTime());
cal.set(Calendar.YEAR,n);
This will add 3 years to the current date and print the year.
System.out.println(LocalDate.now().plusYears(3).getYear());
If you need add one year a any date use the object Calendar.
Calendar dateMoreOneYear = Calendar.getInstance();
dateMoreOneYear.setTime(dateOriginal);
dateMoreOneYear.add(Calendar.DAY_OF_MONTH, 365);
Try like this as well for a just month and year like (June 2019)
Calendar cal = Calendar.getInstance();
cal.add(Calendar.YEAR, n); //here n is no.of year you want to increase
SimpleDateFormat format1 = new SimpleDateFormat("MMM YYYY");
System.out.println(cal.getTime());
String formatted = format1.format(cal.getTime());
System.out.println(formatted);
Try this....
String s = new SimpleDateFormat("YYYY").format(new Date(random_date_in_long)); //
int i = Integer.parseInt(s)+n;