converting long string to date

前端 未结 6 674
盖世英雄少女心
盖世英雄少女心 2020-12-20 02:40

I am getting date value from DB as a long value. I am converting this to string to use parse function. Given below is my code

相关标签:
6条回答
  • 2020-12-20 03:09

    Try this,

    SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
        Date dateD=new Date();
        dateD.setTime(LongTime);
        date=dateFormat.format(dateD);
    
    0 讨论(0)
  • 2020-12-20 03:10

    Try this

    Date date1 = new SimpleDateFormat("MMddyyyySSS").parse(strDate1);
    

    Hope it will works for 12302012235 , but i assume 235 is millisec.

    0 讨论(0)
  • 2020-12-20 03:13

    Java 8, Convert milliseconds long to Date as String by given date format pattern. If you have a long milliseconds and want to convert them into date string at specifies time zone and pattern, then you can use it:-

    dateInMs is a long value of DateTime.

    DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
                        .format(Instant.ofEpochMilli(dateInMs).atZone(ZoneId.of("Europe/London")))
    
    0 讨论(0)
  • 2020-12-20 03:18

    You can try following code:

    private Date getGMTDate(long date) {
        SimpleDateFormat dateFormatGmt = new SimpleDateFormat(
                "yyyy-MMM-dd HH:mm:ss");
        dateFormatGmt.setTimeZone(TimeZone.getTimeZone("GMT"));
        SimpleDateFormat dateFormatLocal = new SimpleDateFormat(
                "yyyy-MMM-dd HH:mm:ss");
    
        Date temp = new Date(date);
    
        try {
            return dateFormatLocal.parse(dateFormatGmt.format(temp));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return temp;
    }  
    

    I hope this will help you.

    0 讨论(0)
  • 2020-12-20 03:24

    Try the following code segment:

            Calendar c = Calendar.getInstance();
            c.setTimeInMillis(Long.parseLong(val));         
            Date d = (Date) c.getTime();        
            SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");       
            String time = format.format(d);//this variable time contains the time in the format of "day/month/year".    
    
    0 讨论(0)
  • 2020-12-20 03:30

    i got the answer.actually i wanted to convert the string to date only for comparing the values.since i am getting the value as long i directly used the compareTo function to do this.avoided the conversion of long to string and string to date conversion.thank you all for support.

    0 讨论(0)
提交回复
热议问题