Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.2k views
in Technique[技术] by (71.8m points)

datetime - Parse yyyy/mm/dd to epoch timestamp using SimpleDateFormat in java

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);
}

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...