Why is my code getting wrong date? [duplicate] - android

This question already has answers here:
How can I get current date in Android?
(31 answers)
Closed 6 years ago.
I am using this code to get current date to string, however, the date it gets is 1899-12-31 how is this possible?
// save date
String date = new SimpleDateFormat("yyyy-MM-dd")
.format(new Date(0, 0, 0));
file.writeToSD("Date: " + date.toString());

0,0,0 is the epoc -- in this case the year 1900
try date();
better take a look at the date page

If you want to retrieve current date then use only use newDate(). Its return current millisecond
In your case you should use bellow code
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String currentDate = dateFormat.format(new Date());
file.writeToSD("Date = " + currenDate);

Related

Date parsing. How do I parse this date format? [duplicate]

This question already has answers here:
Convert JSON date format
(5 answers)
Closed 5 years ago.
How do I parse this date format:
"/Date(1514728800000+0300)/"
I don't know what the meaning of this number is, or that of the + sign.
Partial answer: the number is the epoch time.
This is the amount of seconds since Jan 1, 1970, UTC.
You can pass this value to the constructor of java.util.Date, which will get you a Date object with the right value.
The +0300 is unclear, perhaps a reference to a different timezone.
Im not sure about the +0300, but you can convert a epoch time to Date with the following function:
Date date = new Date(Long.parseLong(myDateToParse.replaceAll("[^\\d-]", "")));
SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss", Locale.DEFAULT);
Here is the code that parses this assuming you have unixtimestamp_zoneoffset
String inputStr = "1514728800000+0300";
String[] splitStr = inputStr.split("[+-]");
String offsetSign = inputStr.indexOf("+")>0 ? "+" : "-";
ZonedDateTime captureTime = Instant.ofEpochMilli(Long.valueOf(splitStr[0])).atZone(ZoneOffset.of(offsetSign+splitStr[1]));
The value returned is 2017-12-31T17:00+03:00

Issue in date comparision using compareTo() method in android [duplicate]

This question already has answers here:
Java date format conversion - getting wrong month
(8 answers)
Getting wrong month when using SimpleDateFormat.parse
(3 answers)
Android SimpleDateFormat, how to use it?
(11 answers)
Closed 5 years ago.
I am trying to compare two dates in android in the 24 hour format following is the date formats but both are giving same result when trying one after other.
SimpleDateFormat simpleDateFormat24Hour = new SimpleDateFormat("mm/dd/yyyy HH:mm");
SimpleDateFormat simpleDateFormat24Hour = new SimpleDateFormat("mm/dd/yyyy HH:mm", Locale.US);
Log.e(startDateEditText.getText().toString().trim());
Log.e(endDateEditText.getText().toString().trim());
Date startDate = simpleDateFormat24Hour.parse(startDateEditText.getText().toString().trim());
Date endDate = simpleDateFormat24Hour.parse(endDateEditText.getText().toString().trim());
if (startDate.compareTo(endDate) > 0) {
showAlertDialog("End date should greater than start date.");
}
following are the inputs for start and end date respectively
04/20/2017 11:07
05/18/2017 11:22
if the end date selection is one month greater or if select next month's date then this issue arises but if date selection is from same month then this works fine.
please suggest some tips to resolve this issue. Thanks in advance.
Try this,
if (false == isDateAfter("04/20/2017 11:07", "05/18/2017 11:22")) {
showAlertDialog("End date should greater than start date.");
}
isDateAfter Method:
public static boolean isDateAfter(String startDate, String endDate) {
try {
String myFormatString = "dd-MM-yyyy"; // for example
SimpleDateFormat df = new SimpleDateFormat(myFormatString, Locale.ENGLISH);
Date endingDate = df.parse(endDate);
Date startingDate = df.parse(startDate);
return endingDate.equals(startingDate) || !endingDate.after(startingDate);
} catch (Exception e) {
return false;
}
}

How to obtain date,day,year,hours and minutes value from a string? [duplicate]

This question already has an answer here:
How to get day, month, year and hour, minutes, second from DateTime format?
(1 answer)
Closed 8 years ago.
I have a field in mysql database of type DateTime.The value in this field is sent as a string to my android app( say in the form "2014-11-21 06:00:00") .I need to obtain year,month,day,hours and minutes value from the string and set it to a calendar instance.Please help me.
String s = "2014-11-21 06:00:00";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar calendar = Calendar.getInstance();
calendar.setTime(simpleDateFormat.parse(s));
System.out.println(calendar.getTime());
To convert your String in a Date object you can use a SimpleDateFormat:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.UK);
Date date = simpleDateFormat.parse("2014-11-21 06:00:00");
Then you can use a Calendar with the Date object you got before
Calendar c = Calendar.geInstance();
c.setTime(date);
and use c.get(Calendar.SECOND) to get the seconds, for instance

How to convert date(String) into Date format(yyyy-MM-dd) in Android? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Am getting current date using date picker. Using StringBuilder it displayed like this (10-08-2014) now I can able to get that in string using String selecteddate=datepick.getText().toString();
selecteddate=10-08-2014 but i need to store in database like this (2014-08-10). How to convert a string value(10-08-2014) inti string value(2014-08-10) ? Help me
Use SimpleDateFormat Class
Date d = Calendar.getInstance().getTime(); // Current time
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // Set your date format
String currentData = sdf.format(d); // Get Date String according to date format
Date cDate = new Date();
String fDate = new SimpleDateFormat("yyyy-MM-dd").format(cDate);
You may use this also
and if you want to set selecteddate=10-08-2014 in yyyy-mm-dd format you can use
String s[] = selecteddate.split("-");
String newdate = s[2]+"-"+s[1]+"-"+s[0];
Check this Code, It suits to your requirement.
String date="10-08-2014";
DateFormat df=new SimpleDateFormat("dd-MM-yyyy");
Date d;
try {
d = df.parse(date);
df=new SimpleDateFormat("yyyy-MM-dd");
String myDate = df.format(d);
Log.i(TAG,myDate);
} catch (ParseException e) {}
Simple date format:
Date date = Calendar.getInstance().getTime();
// Display a date in day, month, year format
DateFormat formatter = new SimpleDateFormat("yyyy MM dd");
String today = formatter.format(date);
System.out.println("Today : " + today);
Link for the code above: Simple Date Format
Code snippet: DatePicker example find more in the link below:
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
// set current date into textview
textView.setText(new StringBuilder()
.append(year) .append(month + 1).append("-").append(day).append("-")
.append(" "));
Here you can find a great example: Mkyong android-date-picker-example

How to fetch time using Time class in Android? [duplicate]

This question already has answers here:
How to get current time and date in Android
(42 answers)
Closed 9 years ago.
I need to get the Current time in a string format in android. I have seen a toString method in Time class. Is there any way to get the current time using Time class or object in android? If not, how can I get the current time as a string in android?
That's how I do it:
Date date = new Date();
java.text.DateFormat dateFormat = android.text.format.DateFormat.getTimeFormat(getBaseContext());
dateFormat.format(date);
There are multiple possibilities:
Automatically use the user-preferred format:
String dateString = DateFormat.getTimeFormat(thisContext).format(Calendar.getInstance().getTime())
Use your own format:
String dateString = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
try to do something like this
Time time = new Time();
System.out.println("time = " + time);
System.out.println("time.toHour() " + time.toHour());
// etc..
// Test with a supplied value
Time time2 = new Time(12033312L);
System.out.println("time2 = " + time2);
also
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
//get current date time with Date()
Date date = new Date();
System.out.println(dateFormat.format(date));
//get current date time with Calendar()
Calendar cal = Calendar.getInstance();
System.out.println(dateFormat.format(cal.getTime()));

Categories

Resources