How do I get today's date to print to the console in Java (2018)? [on hold]

Multi tool use


How do I get today's date to print to the console in Java (2018)? [on hold]
I know this question has been asked a lot, but I've tested code from hundreds of posts about this, and none of it is working for me.
I want to print something like this:
Today's date is: 07/29/2018
Day of Week: Sunday
Here is the code I am currently testing:
package javaDemo;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class TodaysDate {
public static void main(Stringargs) {
System.out.println("Today's date is: "+DateTimeFormatter.ofPattern("MM/dd/yyyy").format(LocalDateTime.now()) + " Day of Week: "+LocalDateTime.now().getDayOfWeek());
}
}
This question appears to be off-topic. The users who voted to close gave this specific reason:
every bit of code I can find on the internet related to storing today's date as a string (whether that's the date on my local machine or the date in the EST time zone) I've tried everything I can possibly find about storing the current date as a string.
– NewGirl
54 mins ago
See this link.
– y2k-shubham
53 mins ago
Can you show us some code? Please read how to ask a good question then edit your question to improve it.
– Opsse
51 mins ago
I edited. Sorry about the formatting. I cant get it to recognize that the imports are also part of the code.
– NewGirl
38 mins ago
2 Answers
2
Try with this if you use java8
:
java8
System.out.println("Today's date is: "+DateTimeFormatter.ofPattern("MM/dd/yyyy").format(LocalDateTime.now()) + " Day of Week: "+LocalDateTime.now().getDayOfWeek());
if you use lower version of java8
java8
String finalDay={"SUNDAY","MONDAY","TUESDAY","WEDNESDAY","THURSDAY","FRIDAY","SATURDAY"};
Calendar c = Calendar.getInstance();
c.setTime(new Date());
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
System.out.println("Today's date is: "+new SimpleDateFormat("MM/dd/yyyy").format(System.currentTimeMillis()) + " Day of Week: "+ finalDay[dayOfWeek]);
Do I need to import something for this?
– NewGirl
45 mins ago
import java.time.LocalDateTime; import java.time.format.DateTimeFormatter;
– sajib
44 mins ago
you need to use java 8
– sajib
44 mins ago
I'm running Java 10.
– NewGirl
43 mins ago
read error. there is problem in your class
– sajib
31 mins ago
LocalDateTime
is not a moment
LocalDateTime
Never use LocalDateTime
when representing a moment. Lacking any concept of time zone or offset, this class represents potential moments along a range of about 26-27 hours, the range of time zones around the world.
LocalDateTime
For a moment, use Instant
, OffsetDateTime
, or ZonedDateTime
.
Instant
OffsetDateTime
ZonedDateTime
Time zone
Specify a time zone. For any given moment, the date varies around the globe by zone.
ZoneId z = ZoneId.of( "Africa/Tunis" ) ;
LocalDate ld = LocalDate.now( z ) ;
If you need the time-of-day as well, use ZonedDateTime
.
ZonedDateTime
ZonedDateTime zdt = ZonedDateTime.now( z ) ;
Generating strings
Specify the formatting pattern.
DateTimeFormatter f = DateTimeFormatter.ofPattern( "MM/dd/uuuu" , Locale.US ) ;
String output = ld.format( f ) ;
Use DayOfWeek
enum. Call getDisplayName
to automatically localize the name of the day of the week.
DayOfWeek
getDisplayName
DayOfWeek dow = ld.getDayOfWeek() ;
String output = dow.getDisplayName( TextStyle.FULL , Locale.US ) ;
"but I've tested code from hundreds of posts about this, and none of it is working for me." - like what code?
– Li357
56 mins ago