I'm trying to run this little code to return a eight-digit integer to be used in a for-loop later on as a search function. Problem is that it doesn't return any values for searchDateToday. Am I missing something?
final Calendar cal = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
DateToday = formatter.format(cal.getTime()); // YYYYMMDD form -
// Example: 20111010
// = October 10,
// 2011
int searchDateToday = Integer.parseInt(DateToday);
You forgot to use the Date object:
final Calendar cal = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
DateToday = formatter.format(new Date(cal.getTime()));
// YYYYMMDD form -// Example: 20111010 // = October 10, // 2011
int searchDateToday = Integer.parseInt(DateToday);
use this for getting today date
String currentDate = DateFormat.getDateInstance().format(new Date());
tv_date.setText(currentDate); // tv_date is TextView
Related
// For Date validation
SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("dd/MM/yyyy");
String datechosen = dateText.getText().toString() ;
Date dateselected = simpleDateFormat1.parse(datechosen);
System.out.println(dateselected);
// For Time validation
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
String timechosen = timeText.getText().toString();
Date timeselected = simpleDateFormat.parse(timechosen);
Calendar cal = Calendar.getInstance();
cal.setTime(timeselected);
cal.add(Calendar.HOUR, noofhourselected);
cal.add(Calendar.MINUTE, noofminselected);
timeselected = cal.getTime();
System.out.println(timeselected);
I am working on converting the string which i have into the date and time format. For example, the string datechosen contain "26/10/2020". I am able to to convert it into date format and print it out.
But for the time string, i am unable to print them out. I am facing the error below:
Screenshot of the log message
But if i swap the position of the codes the other way round,
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
String timechosen = timeText.getText().toString();
Date timeselected = simpleDateFormat.parse(timechosen);
Calendar cal = Calendar.getInstance();
cal.setTime(timeselected);
cal.add(Calendar.HOUR, noofhourselected);
cal.add(Calendar.MINUTE, noofminselected);
timeselected = cal.getTime();
System.out.println(timeselected);
// For Date validation
SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("dd/MM/yyyy");
String datechosen = dateText.getText().toString() ;
Date dateselected = simpleDateFormat1.parse(datechosen);
System.out.println(dateselected);
The time will be printed instead
These are the two input fields
You are setting date in wrong format for time. for cal.setTime(timeselected); setTime takes Date Refer java doc
Use same format as used for Date.
I have an API contains string date with this format "/Date(1527677209864)/", how can I get the date and time to be used in Android app
You can use Date with the epoch number as a parameter in the constructor.
First you have to strip /Date( and )/ from the string, this you can do with regex.
Pattern pattern = Pattern.compile("\\D+([0-9]+)\\D+");
Matcher matcher = pattern.matcher("/Date(1527677209864)/");
if (matcher.matches()) {
long timestamp = Long.parseLong(matcher.group(1));
Date actualDate = new Date(timestamp);
}
I'm assuming that 1527677209864 value is a timestamp, right?
Try this function:
public static String getDateAndTime(#NotNull Context context, long timestamp) {
Date date = new Date(timestamp * 1000);
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
DateFormat df = new SimpleDateFormat(getTimeFormat(context), Locale.getDefault());
return df.format(date);
}
Let me know if this is what you were looking for.
Remove the prefix /Date( and suffix )/. Use the result to initialize a java.util.Date.
String dt = "/Date(1527677209864)/";
dt = dt.substring(6, dt.indexOf(")/"));
long timestamp = Long.parseLong(dt);
Date date = new Date(timestamp);
Use SimpleDateFormat to format this date to your required format.
We have to filter the timeInMillis value from the string and convert it to long so that we can use or set it in calendar and get the date object.
Date convertToDate(String input) {
// input = "/Date(1527677209864)/";
String timeString = input.substring(6, input.length() - 2);
Long time = Long.parseLong(timeString);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(time);
return calendar.getTime();
}
Then we can format the date object to string according to our desire patter or format.
String convertDateToString(Date date, String pattern) {
SimpleDateFormat formatter = new SimpleDateFormat(pattern);
return formatter.format(date);
}
Try this
String jsonDate = "/Date(1527677209864)/";
jsonDate = jsonDate.substring(6, 13);
int unix_timestamp = Integer.parseInt(jsonDate);
Date date = new Date(unix_timestamp);
If the date is 2017-03-30 that i want to fetch the date from 2017-03-23 to 2017-03-30
I try to use this code let my String change to Date format
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date dateParse = sdf.parse("2017-03-30");
then i'm stuck , cause i take the reference is get the current time like this
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, -7);
//may be my dateParse should put here , but i don't know how to do
Date monday = c.getTime();//it get the current time
String preMonday = sdf.format(monday);
Is any one can teach me how to fetch these seven days ? Thanks in advance.
You can use the code below
SimpleDateFormatdateFormat = new SimpleDateFormat("dd MMM yyyy");
Calendar c = Calendar.getInstance();
String date = dateFormat.format(c.getTime());
c.add(Calendar.DATE, 7);
String date1 = dateFormat.format(c.getTime());
Parse the date:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date myDate = sdf.parse("2017-03-30");
First Solution 1) And then either figure out how many milliseconds you need to subtract:
Date newDate = new Date(myDate.getTime() - 604800000L); // 7 * 24 * 60 * 60 * 1000
Second Solution 2) Or use the API provided by the java.util.Calendar class:
Calendar calendar = Calendar.getInstance();
calendar.setTime(myDate);
calendar.add(Calendar.DAY_OF_YEAR, -7);
Date newDate = calendar.getTime();
Then, if you need to, convert it back to a String:
String date = dateFormat.format(newDate);
This answer is from here
EDIT:
If you need output as 2017-03-29 2017-03-28 2017-03-27 ...... 2017-03-23 then try below code
for(int i = 1; i <= 7; i++){
Calendar calendar = Calendar.getInstance();
calendar.setTime(myDate);
calendar.add(Calendar.DAY_OF_YEAR, -i);
Date newDate = calendar.getTime();
String date = dateFormat.format(newDate);
//here in date you can get all date from and output as 2017-03-29 2017-03-28 2017-03-27 ...... 2017-03-23
}
Hope you need this
It's my code for getting today to long.
Can I get yesterday by using this code?
SimpleDateFormat date_0 = new SimpleDateFormat("yyMMdd");
Date date_1 = new Date();
long date_t = Long.valueOf(date_0.format(date_1));
EDIT>
I solve with this
SimpleDateFormat date_0 = new SimpleDateFormat("yyMMdd");
Date date_1 = new Date();
long date_t = Long.valueOf(date_0.format(date_1));
long date_2 = date_1.getTime();
Date yesterday = new Date(date_2 -= 86400000);
long date_y = Long.valueOf(date_0.format(yesterday));
You could take the current date, and subtract 86400000 milliseconds (equivalent to one day).
SimpleDateFormat date_0 = new SimpleDateFormat("yyMMdd");
Date date_1 = new Date();
long date_t = date_1.getTime();
date_t -= 86400000;
Date yesterday = new Date(date_t);
System.out.println("Yesterday's Date: " + date_0.format(yesterday));
I'm not sure but this should work:
SimpleDateFormat date_0 = new SimpleDateFormat("yyMMdd");
Date date_1 = new Date();
date_1.setDate(date_1.getDay()-1);
long date_t = Long.valueOf(date_0.format(date_1));
SimpleDateFormat date_0 = new SimpleDateFormat("yyMMdd");
Calendar calendar = Calendar.getInstance(); // this is default system date
calendar.add(Calendar.DAY_OF_MONTH, -1); // minus date to previous day
long date_t = Long.valueOf(date_0.format(calendar.getTime())); // convert into long
System.out.println(date_0.format(calendar.getTime())); // system print 151110
You can use Joda-Time. The design allows for multiple calendar systems, while still providing a simple API.
private DateTime getPreviousDateAndTime(int previousCount){
DateTimeFormat format = DateTimeFormat.forPattern("yyyy-MM-dd 00:00:00.000000000");
DateTime now = new DateTime();
DateTime expectedDate = now.minusDays(previousCount);
return expectedDate;
}
Add the following dependency to build.gradle:
dependencies {
compile 'net.danlew:android.joda:2.9.0'
}
Simple..
private Calendar calendar;
public void getPreviousDay() {
calendar.add(Calendar.DATE, -1);
}
How do i set the date to 25 -12(december)- current year.
eg.
I am using this code
public static Calendar defaultCalendar() {
Calendar currentDate = Calendar.getInstance();
currentDate.add(Calendar.YEAR,0);
currentDate.add(Calendar.MONTH, 12);
currentDate.add(Calendar.DATE,25);
return currentDate;
}
Something like this should work:
public static Calendar defaultCalendar() {
Calendar currentDate = Calendar.getInstance();
currentDate.set(currentDate.get(Calendar.YEAR),Calendar.DECEMBER,25);
return currentDate;
}
You're trying to add 12 months, instead of setting the month to December (which is month 11, because the Java API is horrible). You want something like:
public static Calendar defaultCalendar() {
Calendar currentDate = Calendar.getInstance();
currentDate.set(Calendar.MONTH, 11); // Months are 0-based!
currentDate.set(Calendar.DAY_OF_MONTH, 25); // Clearer than DATE
return currentDate;
}
Use this it found very usefull to me though :
Take a look at SimpleDateFormat.
The basics for getting the current time in ISO8601 format:
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mmZ");
String now = df.format(new Date());
For other formats:
DateFormat df = new SimpleDateFormat("MMM d, yyyy");
String now = df.format(new Date());
or
DateFormat df = new SimpleDateFormat("MM/dd/yy");
String now = df.format(new Date());
EDit:
Check this link it will help you :
Specific date