How to print day of the given date? [duplicate] - java

This question already has answers here:
How to get Java to show the day of month from a date?
(4 answers)
Closed 6 years ago.
I'm trying to print a day of the given date. How can I do that?
String date = "2015.05.12";
SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd");
Date bornDate = format.parse(date);
System.out.println(bornDate.getDay());//this prints "2" instead of "12"

You can use bornDate.getDate() to get day. You shall use calendar class as methods on date are deprecated.

Related

Timestamp string to Date [duplicate]

This question already has answers here:
How can I change the date format in Java? [duplicate]
(10 answers)
ISO 8601 String to Date/Time object in Android
(5 answers)
Closed 4 years ago.
How to convert the string to a date
In the string we have a timestamp (for example "2018-07-11T04:40:30Z"),
and I want to simply convert this String time to 11-07-2018 16:40:30
String Time = jresponse.getString("timestamp");
SimpleDateFormat sf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
Date date = sf.parse(Time);
I tried many times this code and it doesn't work because I have always this format "2018-07-11T04:40:30Z" and not this 11-07-2018 16:40:30 why ?
If you want to parse dates like "2018-07-11T04:40:30Z"then you should change the format as "dd-MM-yyyy HH:mm:ss" will never work with that date. The correct format should be something like yyyy-MM-dd'T'HH:mm:ssZ.

Take date as input and read month and day [duplicate]

This question already has answers here:
Parse Date from String in this format : dd/MM/yyyy [to dd/MM/yyyy]
(4 answers)
Closed 5 years ago.
so i need to take an input from User in the format of "MM/DD/YYYY"
and later read the day and month from the user input to use in some other calculations
i tried using strings and Gregorian calendar but i have no idea how to do that
Calendar cal = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH);
cal.setTime(dateFormat.parse("12/23/2017"));
Now you can get all kind of metrics form the calendar easily.

I want 7 days ahead of current date by using java 7 not java 8 [duplicate]

This question already has answers here:
Adding days to a date in Java [duplicate]
(6 answers)
Closed 5 years ago.
String s= new java.util.Date().toString();
I am using java 7, I want to print one week ahead of this date I have used the calendar class but it always gives me IST 1970 and does not give correct answer and I want that date into string so please help me in this. I know this question is repeated but they cant solve my query?
Don't use Date, use Calendar:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, 7);
System.out.println(cal.getTime());
If you want date in string format then use Dateformat . Below is the complete code ,
Date givenDate= new Date();
DateFormat dateFormatter=new SimpleDateFormat("dd-MM-yy");
Calendar c=Calendar.getInstance();
c.setTime(givenDate);
//This will print whole week after given date
for(int i=1;i<8;i++){
c.add(Calendar.DAY_OF_MONTH, 1);
System.out.println(dateFormatter.format(c.getTime()));
}

How to get current java date? [duplicate]

This question already has answers here:
How to get the current date/time in Java [duplicate]
(28 answers)
Closed 9 years ago.
How to get current java date? I need to use it in my app for setting posting time. I tried use DateSelecter libdary but that didnt work in the way I needed I to, it printed it in a non human readable way.
You're missing the java tsg. But this is how to get thr date:
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
System.out.println(dateFormat.format(date));

How can I get the day of the specific date in java for android application development? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Android: how to get the current day of the week (Monday, etc…) in user language?
I want to get the date of the specific day in java for android application development.
eg. For Sunday
7.8.2011
14.8.2011
21.8.2011
Date date1 = (new GregorianCalendar(2011 , Calendar.AUGUST, 28)).getTime();
Format formatter = new SimpleDateFormat("EEEE");
String s = formatter.format(date1); // s==Sunday
Take a look at the Calendar Class. You can instantiate it using the Calendar.getInstance() method.
http://download.oracle.com/javase/1.5.0/docs/api/java/util/Calendar.html
i think its something like this cal.get(Calendar.DAY_OF_WEEK)

Categories