使用java中的SimpleDateFormat将yyyy / mm / dd解析为纪元时间戳 [英] Parse yyyy/mm/dd to epoch timestamp using SimpleDateFormat in java

查看:1050
本文介绍了使用java中的SimpleDateFormat将yyyy / mm / dd解析为纪元时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个java实用程序函数来转换yyyy / mm / dd,如下所示

I wrote a java utility function to convert yyyy/mm/dd as follows

public static long gettimestamp(String dateString) {
  SimpleDateFormat df = new SimpleDateFormat("yyyy/mm/dd");
  Date date;
  try {
  date = df.parse(dateString);
  } catch (java.text.ParseException e) {
    return 0;
  }
  long epoch = date.getTime();
  return (epoch / 1000);
}

在通过2014/06/12时 - 它给出1389465360(= 1月11日,这是错误的。我是否以错误的方式传递格式?

On passing 2014/06/12 - it gives 1389465360 (=Jan 11, 2014) which is wrong. Am I passing format in wrong way ?

推荐答案

你应该大写 M 。小写 m 代表分钟,而大写代表月份。这是文档

You should uppercase the M. Lowercase m stands for minutes, while uppercase stands for month. Here's the documentation.

这篇关于使用java中的SimpleDateFormat将yyyy / mm / dd解析为纪元时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆