Java java date and time values ​​from String to Long to String

Long time reader, first time poster.

On Android, fixing the date with datepicker and storing as a string in sqlite. Sorting by date does not work, because these are strings (unless I am mistaken.

I tried this problem for ~ 5 days, and there seems to be a way to grab the date from the selection date, convert it to Long, save it in sqlite as Long, select and sort by Long date value, then convert Long to the string "mm / dd / yyyy "to display. I tried various combinations of parsing expressions, Date, FormatDate, etc. No luck at all.

My actual flow of applications will be: When the activity starts, get the date today and display it in the button that calls datepicker. Record the new date with datepicker (if entered), save it as long-sqlite. When you open an activity that displays a list of records, select from sqlite with orderby in the date (Long), convert Long to the string "mm / dd / yyyy" to display in the ListView.

If someone can point me to a sample code, it would be very helpful - thanks!

Evan

+3
source share
3 answers

Hair extension time later, the solution is a mixture of answers and some other things:

// variables declared
private int mYear;
private int mMonth;
private int mDay;

// datepicker declared and listened for
private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        mYear = year;
        mMonth = monthOfYear;
        mDay = dayOfMonth;
        updateDisplay();
    }
};

// converting the datestring from the picker to a long:
Calendar c = Calendar.getInstance();
c.set(Calendar.DAY_OF_MONTH, mDay);
c.set(Calendar.MONTH, mMonth);
c.set(Calendar.YEAR, mYear);
Long lDate = c.getTime().getTime();

//The formatting that worked on the trip back from long to string 
// (I spent hours with SimpleDateFormat strings, years that were off by 1500, etc.):
String DateFormatted = DateFormat.getDateFormat(getApplicationContext()).format(helper.getDate(c)); // helper.getDate(c) is just the passing back of the Long date from my SELECT statement

- , .

+2

Date java . Long Date Java getTime(), Date . DatePicker , , Java- , Calendar, , Date, Date.

- :

DatePicker dp = blah blah;
Calendar c = Calendar.getInstance();

c.set(Calendar.DAY_OF_MONTH, dp.getDayOfMonth());
c.set(Calendar.MONTH, dp.getMonth());;
c.set(Calendar.YEAR, dp.getYear());

long l = c.getTime().getTime();

. , , , , Date , Calendar.setTime(Date) Calendar.get, , DatePicker.init SimpleDateFormat, , .

+1

, , Calendar . DatePicker:

public void onDateSet(DatePicker view, int year, 
                                  int monthOfYear, int dayOfMonth) {
                mYear = year;
                mMonth = monthOfYear;
                mDay = dayOfMonth;
                updateDisplay();
            }

DatePicker :

Date date = new Date(mYear, mMonth, mDay);

: date.getTime().

Alternatively, if you really have to use strings to sort dates, format them in yyyy / mm / dd format to ensure proper sorting, even then you need to keep track. I just ran some tests with SimpleDateFormat and it parses inappropriate date strings, for example. 2010/09/31 is interpreted as 2010/10/01. When checking such things, I always recommend running some JUnit tests.

+1
source

Source: https://habr.com/ru/post/1767594/


All Articles