java.util.calendar

Java Calendar.DAY_OF_WEEK gives wrong day

好久不见. 提交于 2020-05-29 11:37:08
问题 What is wrong with the below code? It gives wrong day for any date of the year. import java.util.Scanner; import java.util.Calendar; public class Solution { public static String getDay(String d, String m, String y) { String[] days = {"SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY"}; Calendar c = Calendar.getInstance(); c.set(Integer.parseInt(y), Integer.parseInt(m), Integer.parseInt(d)); return days[c.get(Calendar.DAY_OF_WEEK) - 1]; } public static void main

I can't import java.util.date in activity

半世苍凉 提交于 2020-01-16 04:17:19
问题 In my project I need to to use current date and time. For this I'm importing java.util.Date. This import is working fine few hours ago. But I have an error in R file ( r cannot resolve ) and this error is because of some xml mistake. When I resolved that issue and sync project again that error is gone. But after that I cannot able to import (java.util.Date , java.util.Calendar) in any of my project. I tried to import this in my newly created project. But still not found java.util.Date. Even

What is the equivalent of Calendar.roll in java.time?

不想你离开。 提交于 2020-01-11 09:55:30
问题 I was studying the old Calendar API to see how bad it was, and I found out that Calendar has a roll method. Unlike the add method, roll does not change the values of bigger calendar fields. For example, the calendar instance c represents the date 2019-08-31. Calling c.roll(Calendar.MONTH, 13) adds 13 to the month field, but does not change the year, so the result is 2019-09-30. Note that the day of month changes, because it is a smaller field. Related I tried to find such a method in the

How can I take a user inputted date instead of the current date in the Calendar class in Java?

▼魔方 西西 提交于 2020-01-07 04:00:55
问题 I'm working on an assignment, and my goal is to create a class that prints the day of the week given a date. When prompted for input, if the user enters nothing, the program is stopped. Otherwise, if the user enters a date, the program provides the day of the week and then continues to re-prompt the user. The date the user inputs will be in the format of m d y, or for example, 1 10 2017 for January 10th, 2017. What I have so far does everything I need, except it uses the current day, month,

How to get the date of next specified day of week

只愿长相守 提交于 2020-01-04 05:15:46
问题 I have to calculate the next date based on the current date. For example, say today is '05-NOV-2014' and user have gave an input like 1st Monday. so based on the user input if the 1st Monday of the current month has already gone past then i have to find out the 1st Monday of next month. if the 1st Monday is yet to come in the current month then i have to find out the date within the current month. I am using java.util.Calendar class. //user inputs int dayOfWeek = 3; // as user has provided

Setting values of Java Calendar does not give expected date-time

那年仲夏 提交于 2019-12-30 05:59:20
问题 I have an hour, minute, date and millisecond timestamp, and am trying to create a Date object representing the time. The timestamp is provided in Eastern Daylight Time. In dissecting the problem, I created some simple test code to see what was happening and have observed the following: Date today = new Date(); int hour = 4, min = 0, sec = 0, ms = 64; boolean print = true; Calendar cal = GregorianCalendar.getInstance(); if(print) System.out.println("After initializing, time is: "+cal.getTime()

How to get all the dates in a month using calender class?

蹲街弑〆低调 提交于 2019-12-22 08:41:16
问题 Here I want to display dates like 2013-01-01, 2013-01-02, 2013-01-03, . . ...etc I can get total days in a month private int getDaysInMonth(int month, int year) { Calendar cal = Calendar.getInstance(); // or pick another time zone if necessary cal.set(Calendar.MONTH, month); cal.set(Calendar.DAY_OF_MONTH, 1); // 1st day of month cal.set(Calendar.YEAR, year); cal.set(Calendar.HOUR, 0); cal.set(Calendar.MINUTE, 0); Date startDate = cal.getTime(); int nextMonth = (month == Calendar.DECEMBER) ?

Set time to 00:00:00

风格不统一 提交于 2019-12-17 07:13:21
问题 I have a problem resetting hours in Java. For a given date I want to set the hours to 00:00:00. This is my code : /** * Resets milliseconds, seconds, minutes and hours from the provided date * * @param date * @return */ public static Date trim(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Calendar.MILLISECOND, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.HOUR, 0); return calendar.getTime(); } The

Setting Calendar fields in Android

流过昼夜 提交于 2019-12-13 03:46:33
问题 setting the day field in a calendar using myCalender.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY); does is put set the day to the next Friday or the past Friday or maybe the closest? Also what if I don't set the year or any other field for the calendar, what are the default values? 回答1: Neither. Yet. It’s complicated. But don’t use Calendar , see below. From the documentation of the two-arg set method: Sets the given calendar field to the given value. The value is not interpreted by this method

calendar start date before end date on same day difference

☆樱花仙子☆ 提交于 2019-12-11 15:46:15
问题 I have two calendar dates where i am getting the difference between in days, hours, and minutes. This works perfectly if the end date is greater than the start date. What doesnt work is if the start date is the same day of week as the end date, but an earlier time than the end date. For example: end date 2:20 pm Saturday, and start date is 7:20 pm on saturday. It calculates it at like 0days, and 5 hours. But, it should be more like 7 days. Here is the code long t1 = curCal.getTimeInMillis();