我正在尝试将字符串日期转换为长

I am trying to convert a String date to long

我正在将字符串日期转换为 long,但我不明白如何 return long 值 以及如何将字符串转换为长字符串并存储在房间数据库中

package com.example.mybugetssimple;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateConverter {

   public static long dateCon(String string_date){

        string_date = "12-December-2012";

           SimpleDateFormat f = new SimpleDateFormat("dd-MMM-yyyy");
           try {
               Date d = f.parse(string_date);
               long milliseconds = d.getTime();
               return milliseconds;
           } catch (ParseException e) {
               e.printStackTrace();
       }
       return 1;
   }
}

这样使用

  public static long dateCon(String string_date){
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat f = new SimpleDateFormat("dd-MMM-yyyy");
        Date d = null;
        try {
            d = f.parse(string_date);
            calendar.setTime(d);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return calendar.getTimeInMillis();

    }

试试这个

   public static String getStringDateFormTimeStamp(long timestamp) {
    try {
        Calendar calendar = Calendar.getInstance();
        TimeZone tz = TimeZone.getDefault();
        calendar.setTimeInMillis(timestamp);
        calendar.add(Calendar.MILLISECOND, tz.getOffset(calendar.getTimeInMillis()));
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH);
        Date currenTimeZone = calendar.getTime();
        return sdf.format(currenTimeZone);
    } catch (Exception e) {
        Timber.d(e);
    }
    return "";
}


 public static long StringDateToTimeStamp(String string_date){
    Calendar calendar = Calendar.getInstance();
    SimpleDateFormat f = new SimpleDateFormat("dd-MM-yyyy");
    Date d = null;
    try {
        d = f.parse(string_date);
        calendar.setTime(d);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    return calendar.getTimeInMillis();

}