我的分钟变量显示提前900分钟

时间:2017-07-09 19:20:35

标签: java datetime java-time

我的最终结果是:7月3日上午9点18分  2017年9月9日。

918分钟是很奇怪的,我不愿意做那么多的数学计算来确定它的时间。以下是我的代码。

package learn;

import java.time.*;
import java.time.temporal.*;

class time {

    public static void main (String[] args) {

    LocalDateTime now = LocalDateTime.now();

    int minute = now.get(ChronoField.MINUTE_OF_DAY);
    int hour = now.get(ChronoField.HOUR_OF_DAY);
    int day = now.get(ChronoField.DAY_OF_MONTH);
    int month = now.get(ChronoField.MONTH_OF_YEAR);
    int year = now.get(ChronoField.YEAR_OF_ERA);
    int era = now.get(ChronoField.ERA);

    if (hour < 12) {        
        System.out.println("Good morning you sack o' shit."); 
    }
    else if (hour < 17) {
        System.out.println("Get a job hobo. ");
    }
    else {        
        System.out.println("Night night, jobless");
    }

    System.out.print("It's: ");
    if (minute != 0) {

    System.out.print("" + minute + " ");

    System.out.print( (minute != 1) ? "minutes" : "minute");
    System.out.print(" past");

    }
    System.out.print(" ");
    System.out.print( (hour > 12) ? (hour - 12) : hour);
    System.out.print(" o'clock on ");

    switch (month) {        
        case 1: System.out.println("January"); break;
        case 2: System.out.println("February"); break;
        case 3: System.out.println("March"); break;
        case 4: System.out.println("April"); break;
        case 5: System.out.println("May"); break;
        case 6: System.out.println("June"); break;
        case 7: System.out.println("July"); break;
        case 8: System.out.println("August"); break;
        case 9: System.out.println("September"); break;
        case 10: System.out.println("October"); break;
        case 11: System.out.println("November"); break;
        case 12: System.out.println("December"); break;
       }
    System.out.println(" " + day + ", " + year + ", " + era + "st Era.");

    }

}

2 个答案:

答案 0 :(得分:2)

您正在使用using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Data.SQLite; namespace Baby_Tracker { public partial class BabyTracker : Form { BabyEntryForm babyEntryForm = new BabyEntryForm(); BabyUpdateForm babyUpdateForm = new BabyUpdateForm(); BabyDeleteForm babyDeleteForm = new BabyDeleteForm(); public BabyTracker() { InitializeComponent(); comboBoxNameRetrival(); } /* * Allows for the BabyEntryForm to be opened for the user to * add a new entry for a baby. */ private void newBaby_btn_Click(object sender, EventArgs e) { babyEntryForm.Show(); } /* * Allows for the BabyUpdateForm to be opened for the user to * add a updated entry for a baby. */ private void editBaby_btn_Click(object sender, EventArgs e) { babyUpdateForm.Show(); } /* * Calls the FirstName table from the SQLite database to be displayed inside * the combob box. This is also set to refresh as a new baby is entered into the * application. */ public void comboBoxNameRetrival() { DataTable dt = new DataTable(); string connectionString = "Data Source = BabyDatabase.sqlite; Version=3;"; using (SQLiteConnection connection = new SQLiteConnection(connectionString)) { using (SQLiteDataAdapter da = new SQLiteDataAdapter("SELECT FirstName FROM BabyList", connection)) { dt.Clear(); da.Fill(dt); babySelector_cmbo.DisplayMember = "FirstName"; babySelector_cmbo.DataSource = dt; connection.Close(); } } } /* * Used for the selection of the combobox name to be changed throughout the program * as needed. Also, the image for each baby is changed here where the path is called * from the database to then load the image for each baby based off of the selection * from the combobox * * Note: The heart of changing data between differenet babies. */ private void babySelector_cmbo_SelectedIndexChanged(object sender, EventArgs e) { name_lb.Text = babySelector_cmbo.GetItemText(babySelector_cmbo.SelectedItem); //loads the selected name from combobox to theis label string query = "SELECT BabyImagePath FROM BabyList where FirstName = '" + babySelector_cmbo.GetItemText(babySelector_cmbo.SelectedItem) + "'"; string connectionString = "Data Source = BabyDatabase.sqlite; Version=3;"; SQLiteConnection connection = new SQLiteConnection(connectionString); SQLiteCommand command = new SQLiteCommand(query, connection); connection.Open(); SQLiteDataReader reader = command.ExecuteReader(); while (reader.Read()) { string result = Convert.ToString(reader["BabyImagePath"]); userImage_box.Image = Image.FromFile(result); } } /* * Calls the BabyDeleteForm to allow for the user to delete an * baby entry. */ private void deleteBaby_btn_Click(object sender, EventArgs e) { babyDeleteForm.Show(); } } } ,这不是您想要的(下午3:18将相当于当天的918 o 分钟,因为它已经过了918分钟当天(午夜)开始至下午3:18)。

您想要的字段是using System; using System.Collections.Generic; using System.Data; using System.Data.SQLite; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Baby_Tracker { class AddUpdateDeleteBaby { //String declarations public string path; public string targetPath; /* * Method is using the data from the BabyEntryForm textboxes to create a new row for * each new baby entered. The connection and all database requires are set up here. * BabyList is the table that is being used for the storage of the information. */ public void addBabyConnection() { string connectionString = "Data Source = BabyDatabase.sqlite; Version=3;"; using (SQLiteConnection conn = new SQLiteConnection(connectionString)) using (SQLiteCommand command = conn.CreateCommand()) { command.CommandText = "INSERT INTO BabyList (ID, FirstName, MiddleName, LastName, DOB, BirthWeight, BirthLength, BirthHeadCir, BabyImagePath) VALUES (@id, @firstName, @middleName, @lastName, @DOB, @birthWeight, @birthLength, @birthHeadCir, @babyImagePath)"; command.Parameters.AddWithValue("@id", null); command.Parameters.AddWithValue("@firstName", BabyEntryForm.firstName); command.Parameters.AddWithValue("@middleName", BabyEntryForm.middleName); command.Parameters.AddWithValue("@lastName", BabyEntryForm.lastName); command.Parameters.AddWithValue("@DOB", BabyEntryForm.dob); command.Parameters.AddWithValue("@birthWeight", BabyEntryForm.weight); command.Parameters.AddWithValue("@birthLength", BabyEntryForm.length); command.Parameters.AddWithValue("@birthHeadCir", BabyEntryForm.headCir); command.Parameters.AddWithValue("@babyImagePath", BabyEntryForm.imagePath); conn.Open(); command.ExecuteNonQuery(); BabyTracker bt = new BabyTracker(); bt.comboBoxNameRetrival(); } } /* Allows for a image to be selected for the baby entry. This is called via * both new baby entry and also update baby entry. Only JPEG na dPNG images * are accepted here. The files are then saved to a folder (BabyImages) in * the application. */ public void babyImagePath() { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.InitialDirectory = @"C:\"; openFileDialog.Title = "Selet Baby Profile Image"; openFileDialog.Filter = "Image files (*.jpg, *.jpeg, *.png) | *.jpg; *.jpeg; *.png"; openFileDialog.FilterIndex = 2; openFileDialog.RestoreDirectory = true; if (openFileDialog.ShowDialog() == DialogResult.OK) { path = @"C:\Users\Brandon\Documents\Visual Studio 2017\Projects\Baby_Tracker\Baby_Tracker\BabyImages"; //save to file location targetPath = Path.Combine(path, Path.GetFileName(openFileDialog.FileName)); File.Copy(openFileDialog.FileName, targetPath, true); } } /* Connections to the database to use the information entered from the * BabyUpdateForm to update the database based upon the fields that * have entered data. */ public void updateBaby() { string connectionString = "Data Source = BabyDatabase.sqlite; Version=3;"; using (SQLiteConnection conn = new SQLiteConnection(connectionString)) using (SQLiteCommand command = conn.CreateCommand()) { command.CommandText = "UPDATE BabyList SET FirstName = @firstName, MiddleName = @middleName, LastName = @lastName, DOB = @DOB, BirthWeight = @birthWeight, BirthLength = @birthLength, BirthHeadCir = @birthHeadCir, BabyImagePath = @babyImagePath WHERE FirstName = '"+BabyUpdateForm.comboName+"'"; command.Parameters.AddWithValue("@firstName", BabyUpdateForm.updateFirstName); command.Parameters.AddWithValue("@middleName", BabyUpdateForm.updateMiddleName); command.Parameters.AddWithValue("@lastName", BabyUpdateForm.updateLastName); command.Parameters.AddWithValue("@DOB", BabyUpdateForm.updatedob); command.Parameters.AddWithValue("@birthWeight", BabyUpdateForm.updateWeight); command.Parameters.AddWithValue("@birthLength", BabyUpdateForm.updateLength); command.Parameters.AddWithValue("@birthHeadCir", BabyUpdateForm.updateHeadCir); command.Parameters.AddWithValue("@babyImagePath", BabyUpdateForm.updateImagePath); conn.Open(); command.ExecuteNonQuery(); } } /* Connects to the database to allow for the user to delete a baby * entry that they have previously made. Once a baby is deleted * from the database, all data is lost on the baby and must * be reentered as a new baby. */ public void deletebaby() { string connectionString = "Data Source = BabyDatabase.sqlite; Version=3;"; using (SQLiteConnection conn = new SQLiteConnection(connectionString)) using (SQLiteCommand command = conn.CreateCommand()) { command.CommandText = "DELETE FROM BabyList WHERE FirstName = '" + BabyDeleteForm.comboName + "'"; conn.Open(); command.ExecuteNonQuery(); } } } } ,尽管ChronoField.MINUTE_OF_DAY已经有了获取所需字段的方法:

ChronoField.MINUTE_OF_HOUR

要使用英语获取月份名称,还有一种内置方法:

LocalDateTime

答案 1 :(得分:1)

ChronoField Javadoc开始,以下是您需要的字段列表:

MINUTE_OF_HOUR 分钟。

HOUR_OF_DAY 一天一小时。

DAY_OF_MONTH 这个月的日子。

MONTH_OF_YEAR 一年中的月份,例如三月。

YEAR 这个过去的一年,比如2012年。