Win a copy of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 this week in the Programmer Certification forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

Date formating and backwards

 
Ranch Hand
Posts: 1397
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I need to get the current time and the time from yesterday.

for "--date-max" currentdata like 2019-10-11 00:00:00 and for the "--date-min" the now date minus one day.

This is the code I am using for the current time but I missing the code for the date minus 1 day.



Any propoer way to do it?
 
Marshal
Posts: 7271
492
Mac OS X VI Editor BSD Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At least one way is to use a timedelta.
 
Angus Ferguson
Ranch Hand
Posts: 1397
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using it like this;



This line freeze the execution. Could be that (days=-1) is not the rigth way to write it for the day before?
 
Liutauras Vilda
Marshal
Posts: 7271
492
Mac OS X VI Editor BSD Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Angus Ferguson wrote:This line freeze the execution. Could be that (days=-1) is not the rigth way to write it for the day before?


Subtraction you already doing by this: datetime.datetime.now() - datetime.timedelta.... So the delta days need to be defined as 1, and not -1.
 
Angus Ferguson
Ranch Hand
Posts: 1397
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the next code;

I get this error

 
Liutauras Vilda
Marshal
Posts: 7271
492
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So the error is pretty descriptive.

Think for a moment. What exactly you are trying to format? Hint: look at error message carefully. And what exactly you want to format?

Another hint: what parentheses do, let's say in mathematics. i.e.: (1 + 4) * 5
 
Angus Ferguson
Ranch Hand
Posts: 1397
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the working solution I found;

It prints 2019.10.12 00:00:00
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Python, datetime module and a simple line of code we can get the current date in Python.



We can also display just one of the attributes of date in python, such as, just the year.


 
Angus Ferguson
Ranch Hand
Posts: 1397
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes indeed, thanks Deven!
 
Liutauras Vilda
Marshal
Posts: 7271
492
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Angus Ferguson wrote:This is the working solution I found;


I'm not sure why you were trying to implement yourself then if using others solution(s) you find more valuable.

You were very close to your own solution, I exactly specified were you made a mistake, so it was 2 characters (parentheses) to add to fix the problem, but you trashed it all and went to look for somebody else's solution.

 
Angus Ferguson
Ranch Hand
Posts: 1397
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This one is better indeed;
 
Nothing? Or something? Like this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!